简体   繁体   中英

selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH error with Headless Chrome

when i run my script, i got this error

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

here is my script

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

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 .

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

You have to take care of a couple of points here:

  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:\\Utility\\BrowserDrivers\\chromedriver.exe') driver.get("http://www.duo.com") print("Chrome Browser Initialized in Headless Mode") driver.quit() print("Driver Exited")

when i run my script , i got this error

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

here is my script

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH Installing Chromedriver for Windows - selenium.common.exceptions.WebDriverException: Message: 'chromedriver.exe' executable needs to be in PATH Gitlab CI got error selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable needs to be in PATH when I running my simple pro selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH with GeckoDriver Selenium Firefox selenium.common.exceptions.WebDriverException: Message: unknown error: Failed to create a Chrome process error with ChromeDriver Chrome Selenium selenium.common.exceptions.WebDriverException: Message: 'firefox' executable needs to be in PATH with GeckoDriver Firefox Selenium and Python selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally with ChromeDriver Chrome and Selenium selenium.common.exceptions.WebDriverException: Message: unknown error: Failed to create Chrome process with ChromeDriver Chrome with Selenium Python selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed with ChromeDriver and Selenium in Python selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service error using ChromeDriver Chrome through Selenium Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM