selenium初探(chromeDriver + PhantomJS)

selenium API

java selenium 介绍


chromeDriver:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.IOException;

public class Test {
    private static ChromeDriverService service;

    public WebDriver getChromeDriver() throws IOException {
        System.setProperty("webdriver.chrome.driver","src/chromedriver");
        //创建一个 ChromeDriver 接口
        service = new ChromeDriverService.Builder().usingDriverExecutable(new File("src/chromedriver")).usingAnyFreePort().build();
        service.start();
        //创建一个 chrome 浏览器实例
        return new RemoteWebDriver(service.getUrl(), DesiredCapabilities.chrome());
    }

    public static void main(String[] args) throws IOException {
        Test test = new Test();
        WebDriver webDriver = test.getChromeDriver();
        webDriver.get("https://www.baidu.com");
        System.out.println("Page title is: " + webDriver.getTitle());
        WebElement element = webDriver.findElement(By.id("kw"));
        element.sendKeys("暗算");
        element.submit();
        //通过判断 title 内容等待搜索页面加载完毕,间隔秒
        new WebDriverWait(webDriver,10).until(new ExpectedCondition() {

            public Object apply(Object o) {
                return ((WebDriver)o).getTitle().toLowerCase().startsWith("暗算");
            }
        });
        //搜索页面的 title
        System.out.println("Page title is: " + webDriver.getTitle());
        webDriver.quit();
        service.stop();
    }
}

PhantomJS:

  1. PhantomJS的安装(Ubuntu)
  2. github地址

注意: PhantomJS 会报错: phantomjs://platform/console++.js:263 in error
推荐采用 chromeDriver 的 Handless 模式(无头浏览器模式)

import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;

public class PhantomJSTest {

    public PhantomJSDriver getPhantomJSDriver(){
        //设置必要参数
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        //ssl 证书支持
        desiredCapabilities.setCapability("acceptSslCerts",true);
        //截屏支持
        desiredCapabilities.setCapability("takesScreenshot",false);
        //css 搜索支持
        desiredCapabilities.setCapability("cssSelectorsEnabled",true);
        //js 支持
        desiredCapabilities.setJavascriptEnabled(true);
        //驱动支持
        desiredCapabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,"src/phantomjs");

        PhantomJSDriver driver = new PhantomJSDriver(desiredCapabilities);
        return driver;
    }

    @Test
    public void test(){
        WebDriver webDriver = getPhantomJSDriver();
        webDriver.get("http://www.baidu.com");
        System.out.println(webDriver.getCurrentUrl());
    }

}

你可能感兴趣的:(java基础)