樹莓派安裝Firefox+Selenium+geckodriver


相關參考博客
【Selenium】Raspbian+Selenium+Firefox
firefox、geckodriver.exe、selenium-server-standlone版本對應及下載地址
樹莓派如何安裝火狐瀏覽器和geckodriver
geckodriver下載

前提:
最近需要在樹莓派通過Java調用driver然后打開瀏覽器展示某些測試頁面。
一開始通過Chromedriver結合Selenium調用Chrome,本地windows測試一切正常,部署至樹莓派就會出現各種各樣的問題,主要是driver與chrome和Selenium版本兼容問題導致不成功。
后來轉向通過Selenium調用geckodriver控制firefox,成功展示相關頁面。

1.首先樹莓派安裝Firefox

sudo apt-get install firefox-esr -y

  通過以上命令進行firefox安裝,安裝成功后,可通過 whereis firefox 查看火狐瀏覽器安裝路徑.通常情況下在 /usr/bin/firefox 目錄下。

  查看火狐瀏覽器版本:

   firefox -version 

  我的樹莓派安裝的版本為52,例如: Mozilla Firefox 52.*.* 

  另:也可通過ftp mozilla下載對應版本進行安裝。

2.選擇合適的geckodriver

  可通過mozilla/geckodriver下載相關版本的 geckodriver 。

  本地樹莓派火狐瀏覽器版本為52,所以 geckodriver下載 v0.15.0 版本即可geckodriver-v0.15.0-arm7hf.tar.gz

  將下載后的包通過解壓縮命令解壓:

   tar -zxvf geckodriver-v0.15.0-arm7hf.tar.gz 

  將解壓后的文件 geckodriver 復制至 /usr/bin/ 目錄下,並添加執行權限(+x)。

3.選擇相應版本的Selenium

  選擇3.3.1版本的Selenium: selenium-server-standlone-3.3.1.jar .下載地址:http://selenium-release.storage.googleapis.com/index.html

  一開始選擇的是 selenium-java-3.3.1.jar ,調用過程異常,發現依賴進來的相關包的版本卻是3.14.1版本的,例如:selenium-firefox-3.14.1.jar ,修改為 selenium-server-standlone-3.3.1.jar 后正常。

4.測試

System.setProperty("webdriver.gecko.driver","/usr/bin/geckodriver"); FirefoxOptions firefoxOptions = new FirefoxOptions(); List<String> firefoxOp = new ArrayList<String>(10); ..... firefoxOptions.addArguments(firefoxOp); WebDriver firefoxDriver = new FirefoxDriver(firefoxOptions); firefoxDriver.manage().window().maximize(); firefoxDriver.get("https://www.baidu.com"); 

  運行代碼,樹莓派上能夠正常打開火狐瀏覽器並轉向相關測試頁。

5.總結

  • Firefox、selenium、geckodriver最難的是版本兼容,否則各種問題。
  • Firefox、geckodriver、selenium-server-standlone版本對應如下:
selenium-server-standlone-3.3.1.jar      http://selenium-release.storage.googleapis.com/index.html
geckodriver V.15.0                       https://github.com/mozilla/geckodriver/releases
firefox 52.*.*                           http://ftp.mozilla.org/pub/firefox/releases/

6.注

  在使用如下代碼全屏瀏覽器時,會提示異常:firefoxDriver.manage().window().fullscreen(); 

  在 geckodriver 是 0.15.0 版本會報錯,提示: org.openqa.selenium.UnsupportedCommandException: unknown command: session/b368564bbe1863857d7ce10cc5f38e38/window/fullscreen. 

  此處是一個issue,該命令需要 0.17 版本以上,見原文Selenium 3 : Virtual - driver.manage().window().fullscreen() throwing org.openqa.selenium.UnsupportedCommandException.

  This command is only supported in GeckoDriver since 0.17. Please try with latest GeckoDriver and, if it still doesn't work, provide a concise reproducible test case so that we could act on this issue. 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM