selenium 3.0.1遇到問題的解決方案

迄今爲止,我個人認爲,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

WebDriver driver = new FirefoxDriver();
  
driver.get("http://assertselenium.com");

Now – In Selenium – 3

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加入進去,就可以正常的運行了,見執行的源碼:

#!/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!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章