1. 搭建环境之前,要先安装java的jdk、idea工具,请自行安装
Java运行环境–jdk
https://www.oracle.com/java/technologies/javase-jdk11-downloads.html
IDE–idea
https://www.jetbrains.com/idea/download/#section=mac
2.在idea里新建一个maven项目,并且依赖所需要的包(也可以直接下载所需要的jar包导入到Java项目中)
我这里用的是maven项目
直接下一步创建即可。
然后在maven项目中的pom.xml文件中远程依赖所需要的jar包,我是这样写的。(其中也包括了appium所需要的包,不需要可以不用写)
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Test</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0-alpha-4</version>
</dependency>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>7.3.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.junit.jupiter</groupId>-->
<!-- <artifactId>junit-jupiter-api</artifactId>-->
<!-- <version>RELEASE</version>-->
<!-- <scope>compile</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>3.4.0</version>
<!-- <scope>providerd</scope>-->
</dependency>
<!--testng-->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.0.0-beta1</version>
<scope>test</scope>
</dependency>
<!--防止出现 Injector NoClassDefFoundError 的错误 -->
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>3.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
因为在之前搭建环境的时候,远程依赖selenium-server-standalone包总是报错,所以我又在libraries中手动依赖了一个selenium-server-standalone包。
3. 下载chrom对应的chromdriver
这里我使用的浏览器是谷歌
在这里可以看到谷歌的详细版本号
这时去下载我浏览器对应的chromdriver
下载链接:https://chromedriver.storage.googleapis.com/index.html
下载后得到的文件,在访达中打开,把下载好的文件拖动到 /usr/local/bin目录下
打开/usr/local/bin的方法
1 打开mac自带命令行,输入:cd /usr/local/bin
2 再输入:open .则成功打开此目录
这时chromdriver就配置好了,可以打开命令行输入chromedriver --version来看,能返回具体的版本号则表示安装成功~
4.这时就可以编写代码来操作浏览器啦。
这里是打开淘宝的一个简单例子。
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
/**
* @author Arthur
* @Date 2018/2/8
* @Decription:
*/
public class seleniumDemo {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","/usr/local/bin/chromedriver");//这里输入chromedriver的安装目录
ChromeDriver driver = new ChromeDriver();
driver.get("https://www.taobao.com/");
driver.findElement(By.id("q")).click();
driver.findElement(By.id("q")).clear();
driver.findElement(By.id("q")).sendKeys("简笔画文具店");
driver.findElement(By.cssSelector("button.btn-search.tb-bg")).click();
driver.quit();
}
}