0

当我运行我的脚本,我得到这个错误selenium.common.exceptions.WebDriverException:消息:“chromedriver”可执行文件需要在PATH错误铬

Traceback (most recent call last): 
    File "C:\Users\ishaq\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\common\service.py", line 74, in start 
    stdout=self.log_file, stderr=self.log_file) 
    File "C:\Users\ishaq\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 707, in __init__ 
    restore_signals, start_new_session) 
    File "C:\Users\ishaq\AppData\Local\Programs\Python\Python36\lib\subprocess.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:\Users\ishaq\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 62, in __init__ 
    self.service.start() 
    File "C:\Users\ishaq\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\common\service.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:\Users\ishaq\Desktop\chrome\chromedriver.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() 
+1

[错误消息的可能重复:“ 'chromedriver' 可执行文件需要在现有的路径“](https://stackoverflow.com/questions/29858752/error-message-chromedriver-executable-needs-to-be-available-in-the-path) – JeffC

+0

在发布问题之前,您应该花一些时间阅读错误消息并试图理解wha错误是。那么你应该花一些时间搜索错误,以便更好地理解错误的含义以及人们如何解决错误。这是一个常见的错误,如果你花时间去寻找它,那么已经有了一个解决方案。 – JeffC

+0

@JeffC其实我在os.path和binary.location中感到困惑。我搜索了很多关于它,并没有找到解决方案,这就是为什么我发布这个问题。 –

回答

4

如果我们分析日志看来,主要问题是在start os.path.basename(self.path)和后续的错误消息selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH

所以从错误中可以明显看出,Python客户端无法找到二进制文件的chromedriver

你要照顾几个点的位置:

  1. chrome_options.binary_location:参数配置chrome.exe不是chromedriver.exe
  2. os.path.abspath("chromedriver")会拿起文件路径chromedriver但不会追加chromedriver.exe在最后。
  3. 这是我Windows 8系统上的示例代码开始在ChromeHeadless 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:\Utility\BrowserDrivers\chromedriver.exe') 
    driver.get("http://www.duo.com") 
    print("Chrome Browser Initialized in Headless Mode") 
    driver.quit() 
    print("Driver Exited") 
    
+0

其工作,非常感谢先生, –

+0

完成@DebanjanB –

+0

或者,您也可以尝试[**'无头模式下的Mozilla Firefox **](https://www.youtube.com/watch?v=PPS89xCgNhk&t=528s ) – DebanjanB

相关问题