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 楼
如果我们分析日志,似乎主要问题出在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")
评论
另外,您也可以在无头模式下尝试Mozilla Firefox
– DebanjanB
17年9月7日在7:42
我的脚本在本地运行良好,但是我想远程运行我的脚本,我不想整天运行我的机器
–穆罕默德·伊沙克(Muhammad Ishaq)
17年9月7日在20:13
#2 楼
我在Linux中有相同的错误。解决方法非常简单。只需运行此命令,错误就会消失。sudo apt install chromium-chromedriver
评论
错误消息的可能重复:“'chromedriver'可执行文件必须在路径中可用”在发布问题之前,您应该花一些时间阅读错误消息并尝试了解错误是什么。然后,您应该花一些时间搜索错误,以便更好地了解错误的含义以及人们如何解决这些错误。这是一个常见错误,如果您花时间寻找它,那么已经有了解决方案。
@JeffC实际上我对os.path和binary.location感到困惑。我对此进行了大量搜索,但没有找到解决方案,这就是为什么我发布了这个问题。