获取错误 selenium.common.exceptions.WebDriverException: 消息:"chromedriver.exe"可执行文件需要在 python 中的 PATH 中



我在python中收到以下硒错误。我已经通过pip安装硒安装了硒,然后将硒文件提取到C:\Program Files\Python36

这是我的脚本:

import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
dir = os.path.dirname('C:chromedriver_win32')
chrome_driver_path = dir + "chromedriver.exe"
driver = webdriver.Chrome(chrome_driver_path)
driver.implicitly_wait(30)
driver.maximize_window()
driver.get("http://www.google.com")
search_field = driver.find_element_by_name("q")
search_field.send_keys("Selenium WebDriver Interview questions")
search_field.submit()
lists= driver.find_elements_by_class_name("r")
print ("Found " + str(len(lists)) + " searches:")
driver.quit()

一种方法是将 PATH 设置为包含C:chromedriver_win32
但我建议你把chromedriver.exe放在你python.exe的同一个目录中。
设置路径

至于你自己的代码,不妨只用一行:

chrome_driver_path = 'C:\chromedriver_win32\chromedriver.exe'  

无需导入和使用os

另请注意,无论是否os,您都应该转义本身。
例如:

>>> os.path.dirname('C:chromedriver_win32')
'C:\'
>>> os.path.dirname('C:\chromedriver_win32\')
'C:\chromedriver_win32'

最新更新