1,之前因为进行APP自动化,因为Eclipse已经配置过了Maven
2,创建Maven项目
3.引入selenium 框架
查看maven仓库:http://mvnrepository.com/
搜素selenium java
复制到eclipse的pom。xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>come.test</groupId>
<artifactId>WebAuto</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- 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>
</dependencies>
</project>
4.在eclipse->src/test/java创建一个package,再创建一个class
public static void main(String []args) {
//1.打开浏览器chrome
ChromeDriver chromeDriver = new ChromeDriver();
}
解压chromedriver 然后复制到eclipse-》src/test/resource
7.更新代码:
public static void main(String []args) {
System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
//1.打开浏览器chrome
ChromeDriver chromeDriver = new ChromeDriver();
}
public static void main(String []args) {
System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
//1.打开浏览器chrome
ChromeDriver chromeDriver = new ChromeDriver();
//2.访问百度
chromeDriver.get("http://www.baidu.com");
}
报错Java如下
手动IE页面-》缩放改成100%
运行成功
脚本如下:
import org.openqa.selenium.ie.InternetExplorerDriver;
public static void main(String []args) {
//openChrome();
openIE();
}
System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
//1.打开浏览器chrome
ChromeDriver chromeDriver = new ChromeDriver();
//2.访问百度
chromeDriver.get("http://www.baidu.com");
}
public static void openIE() {
System.setProperty("webdriver.ie.driver", "src/test/resources/IEDriverServer.exe");
//1.打开浏览器IE
InternetExplorerDriver IEDriver = new InternetExplorerDriver();
//2.访问百度
IEDriver.get("http://www.baidu.com");
}
}
但是IE自动化过程中遇到的页面缩放比例报错的问题,如果手工改成100%,换其他电脑还是手动执行,不太友好,代码写入解决这个问题
这样就不需要手动IE勾选安全模式和缩放模式
脚本更新如下:
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public static void main(String []args) {
//openChrome();
openIE();
}
System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
//1.打开浏览器chrome
ChromeDriver chromeDriver = new ChromeDriver();
//2.访问百度
chromeDriver.get("http://www.baidu.com");
}
public static void openIE() {
//取消IE安全设置(忽略IE的Protected Mode的设置)
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
//忽略缩放设置
capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
System.setProperty("webdriver.ie.driver", "src/test/resources/IEDriverServer.exe");
//1.打开浏览器IE
InternetExplorerDriver IEDriver = new InternetExplorerDriver(capabilities);
//2.访问百度
IEDriver.get("http://www.baidu.com");
}
}