1 引用jar包
<!-- selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>22.0</version>
</dependency>
2 下载chrome驱动
官网下载:https://chromedriver.storage.googleapis.com/index.html
查看本机chrome版本,大版本对应即可。版本对应参考。
**
windows
linux: google-chrome --version
3 使用
普通使用
System.setProperty("webdriver.chrome.driver", "./chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
//chromeOptions.setHeadless(true);
chromeOptions.addArguments("--disable-gpu");
WebDriver driver = new ChromeDriver(chromeOptions);
try {
driver.get("http://sss");
//WebElement element = driver.findElement(By.cssSelector(".PDF_box"));
//System.out.println(element.toString());
String title = driver.getTitle();
} catch (Exception e) {
e.printStackTrace();
}finally {
driver.close();
}
执行js代码
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.location='https://www.baidu.com/';");