一、Maven包依赖关系图
二、导入Selenium包(高版本包里面已经包含了各平台驱动,如图)
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.12.0</version>
</dependency>
三、以Chrome浏览器为例
/**
* 通过Selenuim启动chrome浏览器
* @author Baopz
* @date 2018/05/24
*/
public class SeleniumApplication {
private static final String base = "https://www.baidu.com";
public static void main(String[] args) {
//设置驱动所在位置
System.setProperty("webdriver.chrome.driver","D:\\chromedriver\\2.37\\chromedriver.exe");
WebDriver driver = new ChromeDriver(initChromeOpts());
driver.get(base);
//做一些事
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
//关闭浏览器
driver.quit();
}
/**
* 设置浏览器所需参数
* @return
*/
private static ChromeOptions initChromeOpts() {
ChromeOptions chromeOptions = new ChromeOptions();
//这里可以不设置浏览器所在位置,这样系统会寻找所需浏览器,如果没有找到,抛错
chromeOptions.setBinary("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
return chromeOptions;
}
}