linux系統下selenium webdriver

1、selenium各版本

selenium各版本 http://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java

2、確認使用的selenium版本及版本對應的firefox

從selenium 3.0.0開始就要求firefox爲48及以上版本,可以通過selenium changelog查看。

changelog查看方式:

(1)打開https://docs.seleniumhq.org/download/,然後 Mozilla GeckoDriver的change log

https://github.com/mozilla/geckodriver/blob/release/CHANGES.md 

(2)由於瀏覽器訪問限制無法打開selenium官網的change log, 可以通過百度搜索selenium changelog去Github上查看。

【Selenium】    -> 【FireFox】

       2.25.0        ->  18
       2.30.0        ->  19
       2.31.0        ->  20
       2.42.2        ->  29
       2.44.0        ->  33 (不支持31)
       2.53.0        ->  43,46(不支持47)
       2.41.0        ->  26(綠色版本)
       2.44          ->  32.0-35.0
       2.53.0-2.53.6 ->  40.0.3(本司目前使用selenium和firefox版本)

3、下載火狐瀏覽器

火狐瀏覽器各版本 http://ftp.mozilla.org/pub/firefox/releases/

4、下載geckodriver

https://github.com/mozilla/geckodriver

https://github.com/mozilla/geckodriver/releases/

下載時一定要確認geckodriver與firefox的版本對應,如版本v0.21.0下的版本說明

Note that with this release of geckodriver the minimum recommended
Firefox and Selenium versions have changed:

  • Firefox 57 (and greater)
  • Selenium 3.11 (and greater)

5、程序代碼:

public static WebDriver getFirefoxDriver(String url) {
        try {
            String osys = System.getProperty("os.name").substring(0,1);
            String geckodriver = "/usr/bin/geckodriver";
            String firefox = "/usr/bin/firefox";
            if("w".equalsIgnoreCase(osys)) {
                geckodriver = "C:\\Program Files\\Mozilla Firefox\\geckodriver.exe";
                firefox = "C:\\Program Files\\Mozilla Firefox\\firefox.exe";
            }
            System.setProperty("webdriver.gecko.driver", geckodriver);
            System.setProperty("webdriver.firefox.bin", firefox);
            DesiredCapabilities capability = DesiredCapabilities.firefox();
            capability.setCapability("marionette", true);
            capability.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);
            WebDriver    driver = new FirefoxDriver(capability);
            //通過driver控制瀏覽器打開鏈接(url)
            driver.get(url);
//            driver.manage().window().maximize();
            return driver;
        } catch (Exception e) {
            System.out.println("oh, no Exception");
            e.printStackTrace();
        }
        return null;
    }

6、報錯 Error: no DISPLAY environment variable specified

沒有Xserver環境。 如果你是ssh遠程到服務器,那麼你必須開啓ssh X forward; 即 ssh 時帶 -X 參數, 同時服務器sshd配置開啓xforward特性; 如果是本地,那你本地就需要先進GUI環境。有xorg環境後,再執行.

 

 

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