簡體   English   中英

selenium.common.exceptions.WebDriverException:消息:“chromedriver”可執行文件需要在無頭 Chrome 的 PATH 錯誤中

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

當我運行我的腳本時,我得到了這個錯誤

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二進制文件。

您必須在這里注意以下幾點:

  1. chrome_options.binary_location :該參數配置的是chrome.exe而不是chromedriver.exe
  2. os.path.abspath("chromedriver")將拿起的文件路徑chromedriver但不會追加chromedriver.exe底。
  3. 這是在我的Windows 8系統上以Headless Mode啟動Chrome的示例代碼:

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

當我運行我的腳本時,我收到了這個錯誤

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 selenium.common.exceptions.WebDriverException:消息:“chromedriver”可執行文件需要在 PATH 中 為 Windows - selenium.common.exceptions.WebDriverException 安裝 Chromedriver:消息:“chromedriver.exe”可執行文件需要在 PATH 中 Gitlab CI 出現錯誤 selenium.common.exceptions.WebDriverException:消息:當我運行我的簡單 pro 時,'chromedriver' 可執行文件需要在 PATH 中 selenium.common.exceptions.WebDriverException:消息:“ geckodriver”可執行文件必須與GeckoDriver Selenium Firefox一起放入PATH selenium.common.exceptions.WebDriverException:消息:未知錯誤:無法使用 ChromeDriver Chrome Selenium 創建 Chrome 進程錯誤 selenium.common.exceptions.WebDriverException:消息:&#39;firefox&#39; 可執行文件需要在 GeckoDriver Firefox Selenium 和 Python 的 PATH 中 selenium.common.exceptions.WebDriverException:消息:未知錯誤:Chrome無法啟動:使用ChromeDriver Chrome和Selenium異常退出 selenium.common.exceptions.WebDriverException:消息:未知錯誤:無法使用帶有Selenium Python的ChromeDriver Chrome創建Chrome進程 selenium.common.exceptions.WebDriverException:消息:未知錯誤:Chrome 無法啟動:在 Python 中使用 ChromeDriver 和 Selenium 崩潰 selenium.common.exceptions.WebDriverException: 消息:無法通過 Selenium Python 使用 ChromeDriver Chrome 連接到服務錯誤
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM