首頁  >  文章  >  後端開發  >  在 Python 中使用 Selenium Chromedriver 設定使用者代理程式時,為什麼我會收到「WebDriverException: \'chromedriver\' Needs to Be in Path\」?

在 Python 中使用 Selenium Chromedriver 設定使用者代理程式時,為什麼我會收到「WebDriverException: \'chromedriver\' Needs to Be in Path\」?

Susan Sarandon
Susan Sarandon原創
2024-11-15 05:03:02139瀏覽

Why Am I Getting \

WebDriverException: 'chromedriver' Needs to Be in Path While Setting UserAgent Through Selenium Chromedriver Python

When attempting to modify your user agent for web scraping using Selenium Chromedriver with Python, you may encounter this 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

Problem:

This error suggests that the ChromeDriver executable was not found in the locations specified within the PATH variable in your Environment Variables.

Solution:

To resolve this issue, pass the "executable_path" key with the absolute path of the ChromeDriver along with the ChromeOptions object as an argument while initializing the WebDriver and WebBrowser:

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')

Ensure that you replace the path with the actual location of the ChromeDriver executable on your system. This will locate the ChromeDriver and initialize the browser with the desired user agent, resolving the error message.

以上是在 Python 中使用 Selenium Chromedriver 設定使用者代理程式時,為什麼我會收到「WebDriverException: \'chromedriver\' Needs to Be in Path\」?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn