【问题标题】:chromedriver wont get website python seleniumchromedriver 无法获取网站 python selenium
【发布时间】:2021-04-27 23:55:22
【问题描述】:

我希望我的程序打开默认的 chrome 配置文件,然后获取 youtube。 我可以让它打开 youtube(在新的 chrome 浏览器中),或者打开默认的 chrome 配置文件,但不能同时打开。 (而且我没有同时运行两个驱动程序变量)

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time


print('starting')
print('getting driver')


exec_path= "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"               # exec path from chrome://version

profilePath= 'C:\\Users\\MyName\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 1'      #profile path from chrome://version

chromePath= 'C:\\Users\\MyName\\OneDrive\\Documents\\Python programming\\chromedriver.exe'  #path to driver



options= webdriver.ChromeOptions()

options.add_argument(profilePath)
print('options add argument...')

### Run one or the other ###
driver = webdriver.Chrome(executable_path= chromePath , options=options) #gets youtube
driver = webdriver.Chrome(executable_path= exec_path, options=options) #gets chrome profile


print('webdriver getting youtube...')
driver.get("https://www.youtube.com/")

当我运行获取 chrome 配置文件的驱动程序行时,我收到错误:

Traceback(最近一次调用最后一次): 文件“C:\Users\MyName\OneDrive\Documents\Python 编程\Web 自动化\webAuto.py”,第 24 行,在 driver = webdriver.Chrome(executable_path= exec_path, options=options) #获取 chrome 配置文件 init 中的文件“C:\Users\MyName\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\chrome\webdriver.py”,第 73 行 self.service.start() 文件“C:\Users\MyName\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\common\service.py”,第 104 行,开始 raise WebDriverException("无法连接到服务 %s" % self.path) selenium.common.exceptions.WebDriverException:消息:无法连接到服务 C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

【问题讨论】:

    标签: python selenium google-chrome selenium-webdriver


    【解决方案1】:

    根据您的问题和代码试验,如果您想使用自定义 Chrome 配置文件打开 Chrome,请尝试使用以下代码。

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    options = Options()
    options.add_argument('C:\\Users\\MyName\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 1' )
    driver = webdriver.Chrome(executable_path='C:\\Users\\MyName\\OneDrive\\Documents\\Python programming\\chromedriver.exe', chrome_options=options)
    driver.get("https://www.youtube.com/")
    

    这里有关于How to open a Chrome Profile through Python的详细讨论

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-20
      • 1970-01-01
      • 2022-12-14
      • 1970-01-01
      • 2020-10-30
      • 1970-01-01
      相关资源
      最近更新 更多