我希望我的程序打开默认的 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 配置文件的驱动程序行时,出现错误:
回溯(最近一次调用最后一次): 文件“C:\Users\MyName\OneDrive\Documents\Python 编程\Web 自动化\webAuto.py”,第 24 行,在 driver = webdriver.Chrome(executable_path= exec_path, options=options) #获取chrome配置文件 文件“C:\Users\MyName\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\chrome\webdriver.py”,第 73 行,init 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
答案 0 :(得分:0)
根据您的问题和您的代码试验,如果您想使用自定义的 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/")
的详细讨论