python selenium selenium-webdriver selenium-chromedriver

python - WebDriverException: Mensaje: el ejecutable ''chromedriver'' debe estar en PATH al configurar UserAgent a través de Selenium Chromedriver Phyton



selenium-webdriver selenium-chromedriver (1)

Soy un novato en webscraping, estoy tratando de modificar mi agente de usuario usando estas líneas:

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)

La ruta en la variable de entorno está bien, pero sigo teniendo este mensaje de error:

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

¿Puedes ayudarme por favor a solucionar este problema?


Este mensaje de error ...

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

... implica que ChromeDriver no se encontró dentro de las ubicaciones especificadas dentro de la variable PATH dentro de las Variables de entorno .

Solución

Debe pasar la ruta ejecutable clave junto con el valor que se refiere a la ruta absoluta de ChromeDriver junto con el objeto ChromeOptions como argumento al inicializar WebDriver y WebBrowser de la siguiente manera:

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