Java程式設計語言下Selenium驅動各個瀏覽器代碼

來源:互聯網
上載者:User

標籤:package   介紹   style   rem   inter   set   windows   imp   cep   

這裡採用的是Selenium3.7版本,首先介紹的是在Windows環境下啟動並執行;

總結下注意事項:

1,設定各個瀏覽器的Driver路徑

System.setProperty("","");

2,建立一個瀏覽器對象

WebDriver driver = new xxx();

 

 

1. 驅動IE瀏覽器

IE瀏覽器的驅動有32位和64位,兩個版本,建議使用32的IEdriver,因為64位的IE driver 跑起來實在太慢了

 1 package base; 2  3 import java.util.concurrent.TimeUnit; 4 import org.openqa.selenium.WebDriver; 5 import org.openqa.selenium.ie.InternetExplorerDriver; 6  7 public class TestIEDriver { 8  9     public static void main(String args[]) throws InterruptedException {10 11         System.setProperty("webdriver.ie.driver", ".\\Tools\\IEDriverServer.exe");12         WebDriver driver = new InternetExplorerDriver();13         driver.manage().window().maximize();14         driver.get("http://www.baidu.com");15         String s = driver.getTitle();16         System.out.print(s);17         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);18         Thread.sleep(1000);19         driver.close();20 21     }22 }

 

2. 驅動Chrome瀏覽器

 1 package base; 2  3 import java.util.Arrays; 4 import java.util.concurrent.TimeUnit; 5  6 import org.openqa.selenium.WebDriver; 7 import org.openqa.selenium.chrome.ChromeDriver; 8 import org.openqa.selenium.chrome.ChromeOptions; 9 import org.openqa.selenium.remote.DesiredCapabilities;10 11 public class TestChromeDriver {12 13     public static void main(String args[]) throws InterruptedException {14 15         System.setProperty("webdriver.chrome.driver", ".\\Tools\\chromedriver.exe");16         ChromeOptions options = new ChromeOptions();17         DesiredCapabilities capabilities = DesiredCapabilities.chrome();18         capabilities.setCapability("chrome.switches", Arrays.asList("--start-maximized"));19         options.addArguments("--test-type", "--start-maximized");20         WebDriver driver = new ChromeDriver(options);21         driver.get("http://www.baidu.com");22 23         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);24         Thread.sleep(1000);25         driver.close();26 27     }28 29 }

 

3. 驅動FirefoxFireFox瀏覽器

 1 package base; 2  3 import java.util.concurrent.TimeUnit; 4  5 import org.openqa.selenium.WebDriver; 6 import org.openqa.selenium.firefox.FirefoxDriver; 7  8 public class TestFireFoxDriver { 9 10     public static void main(String args[]) throws InterruptedException {11 12         System.setProperty("webdriver.Firefox.driver", ".\\Tools\\geckodriver.exe");13         WebDriver driver = new FirefoxDriver();14         driver.manage().window().maximize();15         driver.get("http://www.baidu.com");16         String s = driver.getTitle();17         System.out.print(s);18         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);19         Thread.sleep(1000);20         driver.close();21 22     }23 24 }

 

Java程式設計語言下Selenium驅動各個瀏覽器代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.