webDriver + Firefox 瀏覽器 完美兼容


搞java最煩的就是不同版本的適配問題。現分享下實測成功的案例。

Firefox:4.0.1

selenium:selenium-server-standalone-2.43.1.jar

下面這個鏈接就有以上兩個文件。

http://pan.baidu.com/s/1nvD503f

 

火狐歷史版本下載列表: http://ftp.mozilla.org/pub/firefox/releases/4.0.1/win32/zh-CN/  

 

selenium-server-standalone-2.43.1.jar 需要以文件擴展的形式加入到 Firefox瀏覽器中。

 

package com.****.main;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        // webdriver.chrome.driver C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe

        /*System.setProperty("webdriver.chrome.driver", "C:/Users/Administrator/AppData/Local/Google/Chrome/Application/chrome.exe");
        WebDriver driver = new ChromeDriver();*/

        
        System.setProperty("webdriver.firefox.bin", "C:/Program Files (x86)/Mozilla Firefox/firefox.exe");
        WebDriver driver = new FirefoxDriver();

        //Puts a Implicit wait, Will wait for 10 seconds before throwing exception
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        //Launch website
        driver.navigate().to("http://www.baidu.com/");
        
        //Maximize the browser
        driver.manage().window().maximize();
        
        String strTxt=driver.getPageSource();

        System.out.println(strTxt);
        
        /*// Click on Math Calculators
        driver.findElement(By.xpath(".//*[@id='menu']/div[3]/a")).click();
      
        // Click on Percent Calculators
        driver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a")).click();

        // Enter value 10 in the first number of the percent Calculator
        driver.findElement(By.id("cpar1")).sendKeys("10");

        // Enter value 50 in the second number of the percent Calculator
        driver.findElement(By.id("cpar2")).sendKeys("50");
        
        // Click Calculate Button
        driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr/td[2]/input")).click();

        // Get the Result Text based on its xpath
        String result = driver.findElement(By.xpath(".//*[@id='content']/p[2]/span/font/b")).getText();
        
        //Print a Log In message to the screen
        System.out.println(" The Result is " + result);*/
        
        //Close the Browser.
        driver.close();    
    }

}

 

項目中需要加入上面下載包中的標紅,jar包。

java代碼運行效果如下:

 

轉載請注明出處:http://www.cnblogs.com/jackicalSong/ 

The End.

 


免責聲明!

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



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