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

标签 python selenium selenium-chromedriver headless google-chrome-headless

当我运行脚本时,出现此错误

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

最佳答案

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

因此,从错误中很明显,Python客户端无法找到 chromedriver 二进制文件。

您必须在这里注意以下几点:

  • chrome_options.binary_location :此参数配置 chrome.exe 而不是 chromedriver.exe
  • os.path.abspath("chromedriver")将选择的文件路径chromedriver ,但不会在末尾附加 chromedriver.exe
  • 这是我的Windows 8系统上的示例代码,用于在中启动 Chrome :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")
    
  • 关于python - selenium.common.exceptions.WebDriverException : Message: 'chromedriver' executable needs to be in PATH error with Headless Chrome,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65832842/

    相关文章:

    python - 将二维数组中的像素数据旋转 x 度(伪代码或 Python3)

    python - 在 django 中设置数据库

    python - 按 tf-idf 对 TfidfVectorizer 输出进行排序(从最低到最高,反之亦然)

    HtmlUnit 驱动程序的 Selenium 远程驱动程序问题

    css - 有没有办法使用 c# 和 selenium 缩小 chrome,而不是 CSS 缩放?

    webdriver - Chromedriver 在使用 Selenium Grid 和 .net 的特定计算机上非常慢

    python - ssl.wrap_socket 在go中的实现

    python - 是否可以在 python splinter 中隐藏浏览器窗口(Windows、Linux)?

    python - 使用 Selenium 在 Chrome 中中断异常

    python - Selenium WebDriver等待但仍然是 "Element is not clickable at point"