【问题标题】:WebDriverException: Message: 'chromedriver' executable needs to be in PATH while setting UserAgent through Selenium Chromedriver pythonWebDriverException:消息:“chromedriver”可执行文件需要在 PATH 中,同时通过 Selenium Chromedriver python 设置 UserAgent
【发布时间】:2018-10-10 00:17:30
【问题描述】:

我是网络抓取的新手,我正在尝试使用以下代码来修改我的用户代理:

from selenium import webdriver
chrome_path = r'C:\Users\Desktop\chromedriver_win32\chromedriver.exe'   
driver = webdriver.Chrome(chrome_path)
options = webdriver.ChromeOptions()
options.add_argument('user-agent = Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36')
driver = webdriver.Chrome(chrome_options=options)

环境变量中的路径没有问题,但我一直收到此错误消息:

File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\common\service.py", line 76, in startstdin=PIPE)
File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py",line 709, in __init__restore_signals, start_new_session)
File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py",line 997, in _execute_child startupinfo).
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\safia\AppData\Local\Programs\Python\Python36-32\Test 3- User Agent.py", line 9, in <module>
driver = webdriver.Chrome(chrome_options=options)
File "C:\Users\safia\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 68, in __init__
self.service.start()
File "C:\Users\safia\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\common\service.py", line 83, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

你能帮我解决这个问题吗?

【问题讨论】:

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


【解决方案1】:

此错误消息...

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

...暗示 ChromeDriver环境变量 内的 PATH 变量中指定的位置中找不到。

解决方案

您需要传递 Key executable_path 以及 Value 引用 ChromeDriver 的绝对路径连同 ChromeOptions 对象作为参数,同时初始化 WebDriverWebBrowser,如下所示:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument('user-agent = Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36')
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Users\Desktop\chromedriver_win32\chromedriver.exe')
driver.get('https://www.google.co.in')

【讨论】:

  • 非常感谢DebanjanB
猜你喜欢
  • 1970-01-01
  • 2017-12-08
  • 2022-01-16
  • 2022-12-03
  • 2018-02-14
  • 1970-01-01
  • 1970-01-01
  • 2020-12-06
  • 2018-10-22
相关资源
最近更新 更多