获取错误selenium.common.exceptions.WebDriverException:消息:“chromedriver.exe”可执行文件需要位于python中的路径中

获取错误selenium.common.exceptions.WebDriverException:消息:“chromedriver.exe”可执行文件需要位于python中的路径中,python,selenium,Python,Selenium,python中的selenium出现以下错误。 我已经通过pip安装了selenium,然后将selenium文件解压缩到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

python中的selenium出现以下错误。 我已经通过pip安装了selenium,然后将selenium文件解压缩到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()

一种方法是将路径设置为包含C:\chromedriver\u win32 但我建议您将chromedriver.exe放在python.exe的同一目录中。

至于您自己的代码,最好只使用一行:

chrome_driver_path = 'C:\\chromedriver_win32\\chromedriver.exe'  
不需要导入和使用操作系统

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


已在环境变量中设置了C:\chromedriver\u win32,并尝试将其保存在python.exe所在的同一目录中。@fakhrul Check updated part。不过,我建议您将chromedriver放在python的dir中。并在启动驱动程序时删除chrome\u驱动程序\u路径。@fakhrul抱歉我犯了一个错误,再次更新。
>>> os.path.dirname('C:\chromedriver_win32')
'C:\\'
>>> os.path.dirname('C:\\chromedriver_win32\\')
'C:\\chromedriver_win32'