Reputation: 127
I'm trying to open a website in my default browser with selenium but I can get it to work.
My code:
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\\\Users\\rober\\AppData\\Local\\Google\\Chrome\\User Data")
driver = webdriver.Chrome(executable_path="C:\\\\Users\\rober\\OneDrive\\Skrivebord\\Bot\\chromedriver.exe", chrome_options=options)
driver.get("https://www.zalando.dk/jordan-air-jordan-1-mid-sneakers-high-joc12n001-a18.html")
buyButton = False
while buyButton is False:
try:
addToCartBtn = addButton = driver.find_element_by_xpath('/html/body/div[4]/div/div[2]/div/div/div[2]/div[1]/x-wrapper-re-1-6/div/div[4]/button')
print("Varen er udsolgt")
time.sleep(1)
driver.refresh()
except:
addToCartBtn = addButton = driver.find_element_by_xpath('//*[@id="picker-trigger"]')
print("Varen er på Lager")
buyButton = True
while buyButton is True:
time.sleep(1)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.uc-btn#uc-btn-accept-banner"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Vælg størrelse']"))).click()
driver.execute_script("return arguments[0].scrollIntoView(true);", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//label[starts-with(@for, 'size-picker')]//span[text()='51.5']"))))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//label[starts-with(@for, 'size-picker')]//span[text()='51.5']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Læg i indkøbskurv']"))).click()
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'a[title="Indkøbskurv"]'))).click()
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".z-coast-base__totals-tile .z-1-button__content"))).click()
WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'a[title="Registrér dig"]'))).click()
My Error:
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir
I think the error has something to do with \
but I can not figure it out.
Hope someone can help
Upvotes: 0
Views: 906
Reputation: 193108
This error message...
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir
...implies that the ChromeDriver was unable to initiate/spawn a new Browsing Context i.e. Chrome Browser session for either of the following reasons:
You need to initiate the execution of your program with a clean slate.
Further, while working with Selenium v3.x, ChromeDriver and google-chrome on windows you may need to pass the argument executable_path
along with the absolute path of the ChromeDriver binary through either of the following options:
(\\)
(\)
along with the raw (r)
switch.(.exe)
So you have to change the line :
driver = webdriver.Chrome(executable_path="C:\\\\Users\\rober\\OneDrive\\Skrivebord\\Bot\\chromedriver.exe", chrome_options=options)
with :
driver = webdriver.Chrome(executable_path=r'C:\Users\rober\\OneDrive\Skrivebord\\Bot\chromedriver.exe', options=options)
or :
driver = webdriver.Chrome(executable_path="C:\\Users\\rober\\OneDrive\\Skrivebord\\Bot\\chromedriver.exe", options=options)
You can find a couple of relevant detailed discussions in:
Upvotes: 2