1. chromedriver.exe放入JDK bin目录下:D:\Java\JDK1.8\bin image.png

  2. 引入依赖

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.8.3</version>
        </dependency>
  1. 测试使用
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class Test
{
    public static void main(String[] args) throws InterruptedException
    {
        ChromeDriver driver = new ChromeDriver(new ChromeOptions().addArguments("--remote-allow-origins=*"));
        driver.get("https://www.baidu.com");

        Thread.sleep(20*1000);

        driver.close();
    }
}

image.png

特殊指定

若放入JDK/bin下的chromedriver.exe不起作用,则可在程序中指定该路径:

  • 在配置文件中配置路径:
chromedriver_path: D:/Java/JDK1.8/bin/chromedriver.exe
  • 在程序中设置:
@Value("${chromedriver_path}")
private String chromedriverPath;

System.setProperty("webdriver.chrome.driver", chromedriverPath);