【问题标题】:Opening a browser in own chrome在自己的 chrome 中打开浏览器
【发布时间】:2022-01-17 16:48:30
【问题描述】:

我正在尝试在我自己的 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,但无法成功。

【问题讨论】:

    标签: python selenium google-chrome selenium-chromedriver webdriver


    【解决方案1】:

    试试这个:

    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)
    

    【讨论】:

      【解决方案2】:

      您可以指定 chromedriver 和/或 chrome 的路径,例如:

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

      【讨论】:

        猜你喜欢
        • 2014-04-22
        • 2014-08-06
        • 2017-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-26
        • 2021-06-10
        • 2012-04-21
        相关资源
        最近更新 更多