1 引用jar包

  1. <!-- selenium-java -->
  2. <dependency>
  3. <groupId>org.seleniumhq.selenium</groupId>
  4. <artifactId>selenium-java</artifactId>
  5. <version>3.4.0</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>com.google.guava</groupId>
  9. <artifactId>guava</artifactId>
  10. <version>22.0</version>
  11. </dependency>

2 下载chrome驱动

官网下载:https://chromedriver.storage.googleapis.com/index.html

查看本机chrome版本,大版本对应即可。版本对应参考
**
windows
image.png
linux: google-chrome --version

3 使用

普通使用

  1. System.setProperty("webdriver.chrome.driver", "./chromedriver.exe");
  2. ChromeOptions chromeOptions = new ChromeOptions();
  3. //chromeOptions.setHeadless(true);
  4. chromeOptions.addArguments("--disable-gpu");
  5. WebDriver driver = new ChromeDriver(chromeOptions);
  6. try {
  7. driver.get("http://sss");
  8. //WebElement element = driver.findElement(By.cssSelector(".PDF_box"));
  9. //System.out.println(element.toString());
  10. String title = driver.getTitle();
  11. } catch (Exception e) {
  12. e.printStackTrace();
  13. }finally {
  14. driver.close();
  15. }

执行js代码

  1. JavascriptExecutor js = (JavascriptExecutor) driver;
  2. js.executeScript("window.location='https://www.baidu.com/';");