Selenium学习
Selenium简介
Selenium 是一系列工具和库的综合项目,这些工具和库支持 web 浏览器的自动化。
Selenium安装配置
-
配置Selenium依赖 (Maven项目)
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.141.59</version> </dependency>
-
安装浏览器 (可以参考 官方文档 )
可以自行下载其他浏览器 或者使用系统自带的浏览器
-
安装WebDriver(浏览器驱动)
当没有安装尝试启动项目时,会报以下异常:
Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html at com.google.common.base.Preconditions.checkState(Preconditions.java:847) at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:134) at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:35) at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:159) at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:355) at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:94) at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123) at com.li.Test.main(Test.java:20)
向右边拉动可以发现有链接http://chromedriver.storage.googleapis.com/index.html (以Chrome浏览器为例)
下载自己浏览器对应版本,对应系统的zip文件并解压。
-
在程序中设置System Property
/** * @param: webdriver.chrome.driver(可以查看上面的异常报错获取,如the xxx system property中xxx及配置的系统变量名) * @param: D:\\driver\\chromedriver.exe 即为解压后的driver位置。 */ System.setProperty("webdriver.chrome.driver","D:\\driver\\chromedriver.exe");
Selenium使用
看官方文档—>To Selenium官方文档地址