在自己的 chrome 中打开浏览器

Opening a browser in own chrome

我正在尝试在自己的 chrome 中打开浏览器。我知道我正在使用 Selenium WebDriver,但我想使用我自己的 chrome 浏览器。

我的代码:

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

driver = webdriver.Chrome(executable_path='/path/to/executeable/chrome/driver')


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()

我试过的:

driver = webdriver.Chrome(executable_path='/path/to/executeable/chrome/driver')

错误:

selenium.common.exceptions.WebDriverException: Message: 'driver' executable needs to be in PATH.

我已尝试遵循此 answer,但无法使其正常工作。

试试这个:

import webbrowser

url = 'http://docs.python.org/'

# MacOS
chrome_path = 'open -a /Applications/Google\ Chrome.app %s'

# Windows
# chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'

# Linux
# chrome_path = '/usr/bin/google-chrome %s'

webbrowser.get(chrome_path).open(url)

您可以指定 chrome 驱动程序 and/or chrome 的路径,例如:

chromedriver = '/path/to/chromedriver'
options = Options()
options.binary_location = '/path/to/chrome'
driver = webdriver.Chrome(chromedriver, chrome_options=options)