测试积点老人 发表于 2018-12-27 14:30:24

Python2.7 + selenium3.0配置

安装好了以后在命令行运行Python,引入selenium库,如果没有报错则安装成功,使用.__version__可以查看安装的版本
C:\Users\whats>python
Python 2.7.12 |Anaconda custom (64-bit)| (default, Jun 29 2016, 11:07:13) on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>>import selenium
>>>selenium.__version__
'3.4.3'
使用火狐浏览器进行自动化测试,出现问题
from selenium import webdriver

brow = webdriver.Firefox()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\whats\Anaconda2\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 144, in __init__
self.service.start()
File "C:\Users\whats\Anaconda2\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.
从错误信息知道需要安装’geckodriver’并且设置环境变量
下载geckodriver
http://download.csdn.net/detail/xiaojiawen/9772341
根据自己的电脑选择x86或者x64的进行解压
解压后得到geckodriver.exe,找到Python的安装目录,进入Scripts文件夹,将解压后的文件复制到Scripts文件夹
下一步进行系统环境变量的配置
->右键此电脑->属性->高级系统设置->环境变量

新建系统变量添加以下信息

变量名:geckodriver




输入以下代码进行测试,成功
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox()
browser.get('http://www.baidu.com)
print browser.title #打印网页标题
browser.quit()


页: [1]
查看完整版本: Python2.7 + selenium3.0配置