使用场景:windows环境下利用python3.5.0+Selenium3.4.0+Firefox53.0进行自动化测试
问题:执行自动化脚本报错:
Traceback (most recent call last):
File "C:\Users\Administrator.USER-20160704ZY\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\common\service.py", line 74, in start
stdout=self.log_file, stderr=self.log_file)
File "C:\Users\Administrator.USER-20160704ZY\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 950, in __init__
restore_signals, start_new_session)
File "C:\Users\Administrator.USER-20160704ZY\AppData\Local\Programs\Python\Python35-32\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 "D:/python/代码/baidu.py", line 7, in <module>
driver=webdriver.Firefox()
File "C:\Users\Administrator.USER-20160704ZY\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 142, in __init__
self.service.start()
File "C:\Users\Administrator.USER-20160704ZY\AppData\Local\Programs\Python\Python35-32\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.
解决方法:
从Selenium3.x开始,webdriver/firefox/webdriver.py的_init_中,executable_path=“geckodriver”。
但是Selenium2.x的executable_path="wires"
firefox 47及以上版本需要下载第三方driver,也就是geckodriver。
在https://github.com/mozilla/geckodriver/releases中下载指定操作系统的版本。把下载的压缩包解压后,将解压的路径加入到环境变量path中,重启python即可!
备注:安装三大浏览器驱动driver
1.chromedriver 下载地址:http://www.seleniumhq.org/download/
2.Firefox的驱动geckodriver 下载地址:http://www.seleniumhq.org/download/
3.IE的驱动IEdriver 下载地址:http://www.nuget.org/packages/Selenium.WebDriver.IEDriver/
注意:下载解压后,将chromedriver.exe , geckodriver.exe , Iedriver.exe发到Python的安装目录,例如 D:\python 。 然后再将Python的安装目录添加到系统环境变量的Path下面。
启动三大浏览器
启动谷歌浏览器
from selenium import webdriver browser = webdriver.Chrome() browser.get('http://www.baidu.com/') 启动火狐浏览器from selenium import webdriver browser = webdriver.Firefox() browser.get('http://www.baidu.com/')
启动IE浏览器from selenium import webdriver browser = webdriver.Ie() browser.get('http://www.baidu.com/')