环境:Python3.5 + win10(64bits)+Firefox 52.0.1 (32 位)
安装selenium运行sample
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.baidu.com/')
报错:
H:\Python\Python35\python.exe H:/Python/PycharmProjects/helloworld/SeleniumDemo1.py
Traceback (most recent call last):
File "H:\Python\Python35\lib\site-packages\selenium\webdriver\common\service.py", line 74, in start
stdout=self.log_file, stderr=self.log_file)
File "H:\Python\Python35\lib\subprocess.py", line 950, in __init__
restore_signals, start_new_session)
File "H:\Python\Python35\lib\subprocess.py", line 1220, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "H:/Python/PycharmProjects/helloworld/SeleniumDemo1.py", line 27, in <module>
browser = webdriver.Firefox()
File "H:\Python\Python35\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 144, in __init__
self.service.start()
File "H:\Python\Python35\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: 'geckodriver' executable needs to be in PATH.
解决:(摘自stackoverflow的用户zzy)
First we know that gekodriver is the driver engine of Firefox,and we know thatdriver.Firefox()
is used to open Firefox browser, and it will call the gekodriver engine ,so we need to give the gekodirver a executable permission.so we download the latest gekodriver uncompress the tar packge ,and put gekodriver at the/usr/bin/
ok,that's my answer and i have tested.
需要安装firefox的驱动gekodriver,把gekodriver.exe文件添加到Python的安装目录(即环境变量path里配置的位置,我的是H:\Python\Python35)
在运行sample
最后运行成功,打开百度