Selenium+Java+Chrome进行web自动化实例(初学者)
1.准备工作
第一步 安装JDK
第二步 下载Eclipse
第三步 在Eclipse中安装TestNG
第四步 下载Chrome、chromedriver.exe注意这两个要版本对应,可以搜Chrome与ChromeDriver版本对照表
ChromeDriver 镜像 http://npm.taobao.org/mirrors/chromedriver
2.开始Test
在Eclipse中配置Maven,新建Maven项目,添加依赖
(1)修改Maven的配置文件。下面这两处需要修改
<localRepository>D:\repository</localRepository>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <mirrors> <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors> </settings>
(2)pom添加依赖
<dependencies> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.14.2</version> <scope>test</scope> </dependency> </dependencies>
这里我遇到的问题是我导入了下面的依赖之后,就一直报错Exception in thread “main” java.lang.NoClassDefFoundError: okhttp3/ConnectionPool
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
selenium-java-3.9.x以上版本依赖于okhttp ,但是项目中这个jar包是存在的,不知道什么原因导致的报错,之后换成上面低版本的selenium-java依赖就可以执行了。
新建测试类
(1)先简单测试一下
package com.aia; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class Test { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "E:\\test\\chromedriver_win32\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://www.baidu.com/"); try { Thread.sleep(2*1000); } catch (InterruptedException e) { e.printStackTrace(); } } }
可能会遇到ChromeDriver启动Chrome浏览器后,地址栏只显示data;搜到的解决办法大致是这几种
1、查看是否少了http头部:比如要用 http://localhost:3000 而不是localhost:3000
2、chromeDriver版本不对或者太旧,尝试下载chromeDriver版本对应的chrome浏览器版本
3、可以尝试在代码里加入配置信息:参见下面的方法 options.addArguments();
用了下面的测试类,跑出来的效果是先是打开浏览器的窗口显示data;后续会再次打开浏览器窗口打开代码里的连接。
(2)稍微多点东西,涉及定位页面元素的方法 ,具体可以学习下面链接
https://www.cnblogs.com/hustar0102/p/5965095.html
package com.aia; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeDriverService; import org.openqa.selenium.chrome.ChromeOptions; import org.testng.Assert; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import java.io.File; import java.util.concurrent.TimeUnit; public class LT_TestDemo08 { ChromeDriver driver=null; String chromeBinaryPath = "C:\\Users\\sunflower\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe"; String chromeDriverPath = "E:\\test\\chromedriver_win32\\chromedriver.exe"; @BeforeMethod public void startupDriver(){ ChromeOptions options = new ChromeOptions(); options.setBinary(chromeBinaryPath); options.addArguments(String.format("window-size=%s,%s", 1200, 900)); ChromeDriverService service = new ChromeDriverService.Builder() .usingDriverExecutable(new File(chromeDriverPath)) .usingPort(8999).build(); //.usingAnyFreePort().build(); this.driver = new ChromeDriver(service, options); this.driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); this.driver.manage().deleteAllCookies(); } public void login() throws Exception{ this.driver.get("http://127.0.0.1:8092"); Thread.sleep(2*1000); WebElement webButtonLoginOnIndex = this.driver.findElement(By.xpath("//button[text()='登录']")); webButtonLoginOnIndex.click(); Thread.sleep(2*1000); WebElement webInputUserName = this.driver.findElement(By.xpath("//input[@name='username']")); webInputUserName.sendKeys("wangdong@automation.com"); Thread.sleep(2*1000); WebElement webInputUserPwd = this.driver.findElement(By.xpath("//input[@name='password']")); webInputUserPwd.sendKeys("123"); Thread.sleep(2*1000); WebElement webButtonLogin = this.driver.findElement(By.xpath("//input[@value='登录']")); webButtonLogin.click(); Thread.sleep(2*1000); WebElement webLinkFileUpload = this.driver.findElement(By.xpath("//*[text()='Locators']")); webLinkFileUpload.click(); Thread.sleep(2*1000); } @Test public void test08_tag() throws Exception{ login(); WebElement tagName = this.driver.findElement(By.xpath("//h4[text()='Tag Name']/..")); WebElement element = tagName.findElement(By.tagName("span")); element.click(); Thread.sleep(2*1000); String info = this.driver.findElement(By.xpath("//*[@id='info']")).getText(); Assert.assertTrue(info.equals("标签span")); } @Test public void test08_name() throws Exception{ login(); WebElement element = this.driver.findElement(By.name("xiaolv")); element.click(); Thread.sleep(2*1000); String info = this.driver.findElement(By.xpath("//*[@id='info']")).getText(); Assert.assertTrue(info.equals("小绿")); } @Test public void test08_linkText() throws Exception{ login(); WebElement element = this.driver.findElement(By.linkText("jquery.com")); element.click(); Thread.sleep(2*1000); String info = this.driver.findElement(By.xpath("//*[@id='info']")).getText(); Assert.assertTrue(info.equals("jquery.com")); } @Test public void test08_id() throws Exception{ login(); WebElement element = this.driver.findElement(By.id("mac")); element.click(); Thread.sleep(2*1000); String info = this.driver.findElement(By.xpath("//*[@id='info']")).getText(); Assert.assertTrue(info.equals("mac")); } @Test public void test08_css() throws Exception{ login(); WebElement element = this.driver.findElement(By.cssSelector("#right_div > div > div:nth-child(6) > span.label.label-info")); element.click(); Thread.sleep(2*1000); String info = this.driver.findElement(By.xpath("//*[@id='info']")).getText(); Assert.assertTrue(info.equals("height 65px;")); } @Test public void test08_className() throws Exception{ login(); WebElement element = this.driver.findElement(By.className("classC")); element.click(); Thread.sleep(2*1000); String info = this.driver.findElement(By.xpath("//*[@id='info']")).getText(); Assert.assertTrue(info.equals("classC")); } @Test public void test08_xpath() throws Exception{ login(); WebElement element = this.driver.findElement(By.xpath("//h4[text()='Xpath']/..//*[text()='35 sec.']")); element.click(); Thread.sleep(2*1000); String info = this.driver.findElement(By.xpath("//*[@id='info']")).getText(); Assert.assertTrue(info.equals("35 sec.")); } @Test public void test08_partialLinkText() throws Exception{ login(); WebElement element = this.driver.findElement(By.partialLinkText("旅游")); element.click(); Thread.sleep(2*1000); String info = this.driver.findElement(By.xpath("//*[@id='info']")).getText(); Assert.assertTrue(info.equals("旅游指南")); } @AfterMethod public void shutdownDriver(){ if(this.driver!=null){ try{ //this.driver.close(); this.driver.quit(); }catch (Exception e) { e.printStackTrace(); } } } }
右键 run as TestNG Test ,就会看到浏览器自动打开,并搜索代码的地址,完成代码的动作,浏览器关闭。