问题描述:
执行如下代码
# coding=utf-8 from selenium import webdriver driver = webdriver.Firefox() driver.maximize_window() driver.implicitly_wait(2) driver.get('https://www.baidu.com') try: driver.find_element_by_id('kw') print('test pass: ID found') except Exception as e: print('Exception found', format(e)) driver.quit()
提示报错:
Traceback (most recent call last):
File "E:/PythonSelenium/CSDN/004id.py", line 4, in <module>
driver = webdriver.Firefox()
File "E:Python35libsite-packagesseleniumwebdriverfirefoxwebdriver.py", line 142, in __init__
self.service.start()
File "E:Python35libsite-packagesseleniumwebdrivercommonservice.py", line 81, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
原因分析:
摘取主要报错信息 Message: 'geckodriver' executable needs to be in PATH.
解决方法:
(1)测试环境:Selenium 3.4.0 Firefox 56.0 Python3.5.2
(2)下载火狐浏览器对应驱动,传送门 https://github.com/mozilla/geckodriver/releases 结合我浏览器的版本需要下载V 0.19.0
(3)解压文件获取geckodriver.exe文件,并放置在主目录下。重新运行后发现问题已解决。
注:注意火狐浏览器、Python环境变量设置。