前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >selenium 3.0.1遇到问题的解决方案

selenium 3.0.1遇到问题的解决方案

作者头像
无涯WuYa
发布2018-10-25 16:05:56
1.1K0
发布2018-10-25 16:05:56
举报

迄今为止,我个人认为,selenium是最好使用的web应用程序的自动化测试框架,不仅仅因为它是开源的优势之一,更加重要的是它可以支持的语言比较多,像我们熟悉的java,python,c#等,更加开心的是,selenium官方更新和发布了selenium3.0.1版本,selenium版本都是2.x的。

这里使用的语言是python语言,关于python语言环境的搭建,以及python2.x与python3.x之间的差异,在这里不是重点,如对搭建环境有疑问,可google自己解决。python环境搭建好以后,使用:

pip install -U selenium

命令安装完selenium后,在python的命令行环境,输入:

from selenium import webdriver

driver=webdriver.Firefox()

直接出现错误,具体见错误的截图:

作为初学者来说,出现这样的错误确实很头痛,通过百度来查看错误信息,基本解决不了根本性的问题,在selenium2.x版本中根本不会存在这种情况,期待中的selenium3.0安装好后,第一次尝试想打开浏览器,给人结果是一盘冷水,根本不知道解决的方向是什么?我想说的是,遇到这种问题,不要着急,先来看错误信息,来逐步的慢慢分析,逐步的慢慢解决,Message中提示:Expected browser binary location,but unable to find binary in default location,大概意思就是说寻找不到binary,OK,明白了这点,总的结果来说,selenium3.0不支持firefox默认的driver了,但是在selenium2.x版本是可以支持的,这就是差异,到http://www.assertselenium.com/selenium-3/firefoxdriver-in-selenium-3/地址我们可以获取到如下的信息为:

Selenium 3 is released and there is a lot of changes that are implemented, and one such major change is the firefox browser

implementation using GeckoDriver.

Let’s see How to use FirefoxDriver in Selenium 3

FirefoxDriver was the only driver that was simple to use without any executable configuration setup like chrome or ie. Now, in Selenium 3 you will

have to add an executable to its path like how you set system.setproperty. That’s the way going forward as Browser vendors will take care of their

own Driver implementation.

依据如上的信息,可以看到,selenium3.0在firefox浏览器中提供了GeckDriver,需要使用它,也就是说之前的默认自带的driver在selenium30.已经不能使用了,需要使用GeckDriver,GeckDriver代替了之前的自带默认的driver,这样的好处是每个浏览器都可以自己开发driver,来适配selenium3.0来进行自动化的测试,接着继续往下看,可以看到,提供了GeckDriver的下载地址,见原文:

Earlier – In Selenium – 2

代码语言:javascript
复制
WebDriver driver = new FirefoxDriver();
  
driver.get("http://assertselenium.com");

Now – In Selenium – 3

代码语言:javascript
复制
System.setProperty("webdriver.firefox.driver","your path to the executable");
WebDriver driver = new FirefoxDriver();

见GeckDriver的下载地址https://github.com/mozilla/geckodriver/releases,把GeckDriver在该地址下载后,加入到环境变量,该下载地址提供了

不同平台的文件,见截图:

本人是win7环境,下载geckodriver-v0.9.0-win64.zip文件后,把geckodriver.exe文件放在了C:\Python27目录中(C:\Python27目录已经加入到了环境变量)。把GeckDriver加入到环境变量后,接着来看,GeckDriver具体是什么?见原话:

GeckoDriver acts as a proxy between the W3c compatible Gecko based browsers like Firefox(48 & up). It provides HTTP API

as described by the WebDriver Protocol to communicate to Firefox. It translates calls into the Marionette automation protocol

by acting as a proxy between the local- and remote ends.

上面的原话中,可以看到,想使用GeckoDriver ,firefox浏览器必须是48或者更高版本,那么现在开始干什么?升级firefox浏览器,保障浏览器版本在48或者更高,升级后,把binary加入进去,就可以正常的运行了,见执行的源码:

代码语言:javascript
复制
#!/usr/bin/env python
# -*- coding:utf-8 -*-
 
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
import  time as t
 
 
binary = FirefoxBinary('C:\\Program Files (x86)\\Mozilla Firefox\\Firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary)
driver.get('http://www.baidu.com')
driver.find_element_by_id('kw').send_keys('selenium')
t.sleep(3)
driver.quit()

执行OK!

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2016-11-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python自动化测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档