马猴烧酒 发表于 2018-9-19 08:14:21

selenium.common.exceptions.WebDriverException:消息:'chromedriver'可执...

当我运行我的脚本时,我收到了这个错误
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: 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

I_Like_AI 发表于 2018-9-19 08:16:02

分析日志,主要问题是 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")
页: [1]
查看完整版本: selenium.common.exceptions.WebDriverException:消息:'chromedriver'可执...