Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

WebDriverException: Message: 'chromedriver' executable needs to be in PATH while setting UserAgent through Selenium Chromedriver python

I'm a newbie in webscraping, I'm trying to modify my user agent using these lines :

from selenium import webdriver
chrome_path = r'C:UsersDesktopchromedriver_win32chromedriver.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)

The path in environment variable is ok but I keep having this error message:

File "C:UsersAppDataLocalProgramsPythonPython36-32libsite-packagesseleniumwebdrivercommonservice.py", line 76, in startstdin=PIPE)
File "C:UsersAppDataLocalProgramsPythonPython36-32libsubprocess.py",line 709, in __init__restore_signals, start_new_session)
File "C:UsersAppDataLocalProgramsPythonPython36-32libsubprocess.py",line 997, in _execute_child startupinfo).
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:UserssafiaAppDataLocalProgramsPythonPython36-32Test 3- User Agent.py", line 9, in <module>
driver = webdriver.Chrome(chrome_options=options)
File "C:UserssafiaAppDataLocalProgramsPythonPython36-32libsite-packagesseleniumwebdriverchromewebdriver.py", line 68, in __init__
self.service.start()
File "C:UserssafiaAppDataLocalProgramsPythonPython36-32libsite-packagesseleniumwebdrivercommonservice.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

Can you please help me fix this issue?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This error message...

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

...implies that the ChromeDriver was not found within the locations specified within PATH variable within Environment Variables.

Solution

You need to pass the Key executable_path along with the Value referring to the absolute path of the ChromeDriver along with the ChromeOptions object as an argument while initializing the WebDriver and WebBrowser as follows :

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:UsersDesktopchromedriver_win32chromedriver.exe')
driver.get('https://www.google.co.in')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...