Chrome Selenium在发送cookie后无法获取自定义配置文件或与之交互(以及其他常见解决方案)?

332nm8kg  于 2023-04-09  发布在  Go
关注(0)|答案(1)|浏览(174)
#profile
chrome_options.add_argument(r"user-data-dir=C:\Users\prodi\AppData\Local\Google\Chrome\User Data\Profile 2") 
#Path to your chrome profile

service = ChromeService(executable_path=r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe')

driver = webdriver.Chrome(service=service,options=chrome_options)

driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")

#get url
tempUrl = "https://www.indeed.com/jobs?q=Web+Dev+Intern&l=Remote&sc=0kf%3Aattr%28DSQF7%29explvl%28ENTRY_LEVEL%29%3B&rbl=Remote&jlid=aaa2b906602aa8f5&pp=gQAAAAABhyaEcDMAAAAB_d4VZAADAAABAAA&vjk=e8d531753a776d38"

driver.get(tempUrl)

pickle.dump(driver.get_cookies(), open("cookies.pkl", "wb"))

你好,我试图去一个网址与我的谷歌个人资料。它不工作。 chrome 个人资料弹出,但它dosent做任何事情。过了一段时间,它只是给我一个无法连接错误。
这一点:

Traceback (most recent call last):
  File "c:\Users\prodi\Desktop\auto job applying bot\auto-job-applying-bot\Test Code\main.py", line 52, in <module>
    driver = webdriver.Chrome(service=service,options=chrome_options)
  File "C:\Users\prodi\Desktop\auto job applying bot\auto-job-applying-bot\env\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 80, in __init__    
    super().__init__(
  File "C:\Users\prodi\Desktop\auto job applying bot\auto-job-applying-bot\env\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 101, in __init__ 
    self.service.start()
  File "C:\Users\prodi\Desktop\auto job applying bot\auto-job-applying-bot\env\lib\site-packages\selenium\webdriver\common\service.py", line 113, in start        
    raise WebDriverException(f"Can not connect to the Service {self.path}")      
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

我试过很多解决办法
第一步是确保路径是正确的。它们看起来是https://imgur.com/a/PFVAPgt
第二个:一个解决方案说使用cookie.什么也没做Selenium: get() not working with custom google profile
其他人也注意到什么是错的吗?

z2acfund

z2acfund1#

问题似乎源于这一行:

service = ChromeService(executable_path=r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe')
driver = webdriver.Chrome(service=service,options=chrome_options)

因为您正在使用Chrome浏览器可执行文件创建ChromeService示例,尽管您应该使用Chromedriver可执行文件。请参阅相关docs
我建议你做一个修复,因为每当你的Chrome浏览器更新将使用webdriver-manager包时,总是手动升级你的Chromedriver版本是一件痛苦的事情。代码看起来像这样:

from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options)

相关问题