51Testing软件测试论坛

标题: Python2.7 + selenium3.0配置 [打印本页]

作者: 测试积点老人    时间: 2018-12-27 14:30
标题: Python2.7 + selenium3.0配置
安装好了以后在命令行运行Python,引入selenium库,如果没有报错则安装成功,使用.__version__可以查看安装的版本
  1. C:\Users\whats>python
  2. Python 2.7.12 |Anaconda custom (64-bit)| (default, Jun 29 2016, 11:07:13) [MSC v.1500 64 bit (AMD64)] on win32
  3. Type "help", "copyright", "credits" or "license" for more information.
  4. Anaconda is brought to you by Continuum Analytics.
  5. Please check out: http://continuum.io/thanks and https://anaconda.org
  6. >>>import selenium
  7. >>>selenium.__version__
  8. '3.4.3'
复制代码
使用火狐浏览器进行自动化测试,出现问题
  1. from selenium import webdriver

  2. brow = webdriver.Firefox()
  3. Traceback (most recent call last):
  4. File "<stdin>", line 1, in <module>
  5. File "C:\Users\whats\Anaconda2\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 144, in __init__
  6. self.service.start()
  7. File "C:\Users\whats\Anaconda2\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
  8. os.path.basename(self.path), self.start_error_message)
  9. 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

[attach]120496[/attach]


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

  3. browser = webdriver.Firefox()
  4. browser.get('http://www.baidu.com)
  5. print browser.title #打印网页标题
  6. browser.quit()
复制代码







欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2