selenium.common.exceptions.WebDriverException:消息:“chromedriver"可执行文件需要在无头 Chrome 的 PATH 错误中 [英] selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH error with Headless Chrome

查看:34
本文介绍了selenium.common.exceptions.WebDriverException:消息:“chromedriver"可执行文件需要在无头 Chrome 的 PATH 错误中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行我的脚本时,我收到了这个错误

when i run my script , i got this error

Traceback (most recent call last):
  File "C:UsersishaqAppDataLocalProgramsPythonPython36libsite-packagesseleniumwebdrivercommonservice.py", line 74, in start
    stdout=self.log_file, stderr=self.log_file)
  File "C:UsersishaqAppDataLocalProgramsPythonPython36libsubprocess.py", line 707, in __init__
    restore_signals, start_new_session)
  File "C:UsersishaqAppDataLocalProgramsPythonPython36libsubprocess.py", line 992, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/ishaq/AppData/Local/Programs/Python/Python36/headless.py", line 9, in <module>
    driver = webdriver.Chrome(executable_path=os.path.abspath("chromedriver"),   chrome_options=chrome_options)
  File "C:UsersishaqAppDataLocalProgramsPythonPython36libsite-packagesseleniumwebdriverchromewebdriver.py", line 62, in __init__
    self.service.start()
  File "C:UsersishaqAppDataLocalProgramsPythonPython36libsite-packagesseleniumwebdrivercommonservice.py", line 81, 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

这是我的脚本

import os  
from selenium import webdriver  
from selenium.webdriver.common.keys import Keys  
from selenium.webdriver.chrome.options import Options 

chrome_options = Options()  
chrome_options.add_argument("--headless")  
chrome_options.binary_location = 
r'C:UsersishaqDesktopchromechromedriver.exe'    
driver = webdriver.Chrome(executable_path=os.path.abspath("chromedriver"),   
chrome_options=chrome_options)  
driver.get("http://www.duo.com") 

magnifying_glass = driver.find_element_by_id("js-open-icon")  
if magnifying_glass.is_displayed():  
  magnifying_glass.click()  
else:  
  menu_button = driver.find_element_by_css_selector(".menu-trigger.local")  
  menu_button.click() 

search_field = driver.find_element_by_id("site-search")  
search_field.clear()  
search_field.send_keys("Olabode")  
search_field.send_keys(Keys.RETURN)  
assert "Looking Back at Android Security in 2016" in driver.page_source
driver.close()

推荐答案

如果我们分析日志,似乎主要问题在于 start os.path.basename(self.path) 和后续错误消息 selenium.common.exceptions.WebDriverException: 消息:'chromedriver' 可执行文件需要在 PATH 中.

If we analyze the logs it seems the main issue is with in start os.path.basename(self.path) and subsequent error message selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH.

因此从错误中可以清楚地看出 Python 客户端无法定位 chromedriver 二进制文件.

So it's clear from the error that the Python client was unable to locate the chromedriver binary.

您必须注意以下几点:

  1. chrome_options.binary_location :该参数配置的是 chrome.exe 而不是 chromedriver.exe
  2. os.path.abspath("chromedriver") 将选取 chromedriver 的文件路径,但不会附加 chromedriver.exe 最后.
  3. 这是在我的 Windows 8 系统上以 Headless 模式启动 Chrome 的示例代码:

  1. chrome_options.binary_location : The parameter configures the chrome.exe not the chromedriver.exe
  2. os.path.abspath("chromedriver") will pick up the file path of chromedriver but won't append chromedriver.exe at the end.
  3. Here is the sample code on my Windows 8 system to start Chrome in Headless Mode:

from selenium import webdriver  
from selenium.webdriver.chrome.options import Options 

chrome_options = Options()  
chrome_options.add_argument("--headless")  
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=r'C:UtilityBrowserDriverschromedriver.exe')  
driver.get("http://www.duo.com") 
print("Chrome Browser Initialized in Headless Mode")
driver.quit()
print("Driver Exited")

这篇关于selenium.common.exceptions.WebDriverException:消息:“chromedriver"可执行文件需要在无头 Chrome 的 PATH 错误中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆