Selenium WebDriver JAVA 入門實例

假設 Chrome/Firefox/IE 瀏覽器安裝的是 32 位版本,對應的瀏覽器驅(qū)動放在 D:\WebDrivers 目錄。

Chrome 瀏覽器

import org.openqa.selenium.WebDriver; // 瀏覽器驅(qū)動操作接口
import org.openqa.selenium.By; // 頁面元素定位類
import org.openqa.selenium.chrome.ChromeDriver; // Chrome 瀏覽器驅(qū)動類

public class SeleniumChrome {

    public static void main(String[] args) throws InterruptedException {
        // 設置 Chrome 瀏覽器可執(zhí)行文件的位置,一般可以不用設置
        String browserPath = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe";
        System.setProperty("webdriver.chrome.bin", browserPath);        
        // 設置 Chrome 瀏覽器驅(qū)動的位置
        String driverPath = "D:\\WebDrivers\\chromedriver.exe";
        System.setProperty("webdriver.chrome.driver", driverPath);
        // 關閉 Chrome 瀏覽器驅(qū)動的日志輸出
        System.setProperty("webdriver.chrome.silentOutput", "true");
        // 創(chuàng)建 Chrome 瀏覽器驅(qū)動對象
        WebDriver driver = new ChromeDriver();
        // 打開百度首頁
        driver.get("https://www.baidu.com");
        // 定位搜索輸入框輸入查找的內(nèi)容
        driver.findElement(By.id("kw")).sendKeys("Chrome");
        // 等待 3 秒
        Thread.sleep(3000);
        // 關閉瀏覽器窗口
        driver.close();
    }

}

Firefox 瀏覽器

import org.openqa.selenium.WebDriver; // 瀏覽器驅(qū)動操作接口
import org.openqa.selenium.By; // 頁面元素定位類
import org.openqa.selenium.firefox.FirefoxDriver; // Firefox 瀏覽器驅(qū)動類

public class SeleniumFirefox {

    public static void main(String[] args) throws InterruptedException {    
        // 設置 Firefox 瀏覽器可執(zhí)行文件的位置,一般可以不用設置
        String browserPath = "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe";
        System.setProperty("webdriver.firefox.bin", browserPath);
        // 設置 Firefox 瀏覽器驅(qū)動的位置
        String driverPath = "D:\\WebDrivers\\geckodriver.exe";
        System.setProperty("webdriver.gecko.driver", driverPath);
        // 默認日志信息輸出很多,可以關閉日志
        System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"/dev/null");
        // 創(chuàng)建 Firefox 瀏覽器驅(qū)動對象
        WebDriver driver = new FirefoxDriver();
        // 打開百度首頁
        driver.get("https://www.baidu.com");
        // 定位搜索輸入框輸入查找的內(nèi)容
        driver.findElement(By.id("kw")).sendKeys("Firefox");
        // 等待 3 秒
        Thread.sleep(3000);
        // 關閉瀏覽器窗口
        driver.close();
    }

}

IE 瀏覽器

import org.openqa.selenium.WebDriver; // 瀏覽器驅(qū)動操作接口
import org.openqa.selenium.By; // 頁面元素定位類
import org.openqa.selenium.ie.InternetExplorerDriver; // IE 瀏覽器驅(qū)動類

public class SeleniumIE {

    public static void main(String[] args) throws InterruptedException {
        // 設置 IE 瀏覽器可執(zhí)行文件的位置,一般可以不用設置
        String browserPath = "C:\\Program Files (x86)\\Internet Explorer\\iexplore.exe";
        System.setProperty("webdriver.ie.bin", browserPath);
        // 設置 IE 瀏覽器驅(qū)動的位置
        String driverPath = "D:\\WebDrivers\\IEDriverServer.exe";
        System.setProperty("webdriver.ie.driver", driverPath);
        // 創(chuàng)建 IE 瀏覽器驅(qū)動對象
        WebDriver driver = new InternetExplorerDriver();
        // 打開百度首頁
        driver.get("https://www.baidu.com");
        // 定位搜索輸入框輸入查找的內(nèi)容
        driver.findElement(By.id("kw")).sendKeys("IE");
        // 等待 3 秒
        Thread.sleep(3000);
        // 關閉瀏覽器窗口
        driver.close();
    }

}
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容

  • 目錄: Android:Android 0.*Android 1.*Android 2.*Android 3.*A...
    敲代碼的令狐蔥閱讀 4,027評論 0 2
  • ??JavaScript 與 HTML 之間的交互是通過事件實現(xiàn)的。 ??事件,就是文檔或瀏覽器窗口中發(fā)生的一些特...
    霜天曉閱讀 3,526評論 1 11
  • Selenium 官網(wǎng)Selenium WebDriver官網(wǎng)webdriver實用指南python版本 WebD...
    顧顧314閱讀 47,054評論 0 34
  • 只是用戶看到僅僅只是瀏覽器本身,卻很少能看到瀏覽器最核心的部分—瀏覽器內(nèi)核。從第一款libwww(Library ...
    孫悟空SUN閱讀 2,356評論 0 2
  • 中午休息的時候,方澤問道:“郝師傅,等咱們安裝完了,是您在這負責呢還是要去干別的工作呢?帶著我們唄!” 郝師傅笑了...
    文心聞意閱讀 80評論 0 1