selenium.common.exceptions.WebDriverException:消息:'chromedriver' 可执行文件需要在 Headless Chrome 的 PATH 错误中

2024-12-02 08:41:00
admin
原创
138
摘要:问题描述:当我运行脚本时,出现此错误Traceback (most recent call last): File "C:UsersishaqAppDataLocalProgramsPythonPython36libsite-packagesseleniumwebdrivercommonserv...

问题描述:

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

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

解决方案 1:

如果我们分析日志,似乎主要问题在于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系统启动google-chrome-headless:

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")
相关推荐
  政府信创国产化的10大政策解读一、信创国产化的背景与意义信创国产化,即信息技术应用创新国产化,是当前中国信息技术领域的一个重要发展方向。其核心在于通过自主研发和创新,实现信息技术应用的自主可控,减少对外部技术的依赖,并规避潜在的技术制裁和风险。随着全球信息技术竞争的加剧,以及某些国家对中国在科技领域的打压,信创国产化显...
工程项目管理   1590  
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   1361  
  信创产品在政府采购中的占比分析随着信息技术的飞速发展以及国家对信息安全重视程度的不断提高,信创产业应运而生并迅速崛起。信创,即信息技术应用创新,旨在实现信息技术领域的自主可控,减少对国外技术的依赖,保障国家信息安全。政府采购作为推动信创产业发展的重要力量,其对信创产品的采购占比情况备受关注。这不仅关系到信创产业的发展前...
信创和国产化的区别   18  
  信创,即信息技术应用创新产业,旨在实现信息技术领域的自主可控,摆脱对国外技术的依赖。近年来,国货国用信创发展势头迅猛,在诸多领域取得了显著成果。这一发展趋势对科技创新产生了深远的推动作用,不仅提升了我国在信息技术领域的自主创新能力,还为经济社会的数字化转型提供了坚实支撑。信创推动核心技术突破信创产业的发展促使企业和科研...
信创工作   18  
  信创技术,即信息技术应用创新产业,旨在实现信息技术领域的自主可控与安全可靠。近年来,信创技术发展迅猛,对中小企业产生了深远的影响,带来了诸多不可忽视的价值。在数字化转型的浪潮中,中小企业面临着激烈的市场竞争和复杂多变的环境,信创技术的出现为它们提供了新的发展机遇和支撑。信创技术对中小企业的影响技术架构变革信创技术促使中...
信创国产化   19  
热门文章
项目管理软件有哪些?
云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用