首先创建工程添加selenium依赖
这里使用的chrome浏览器编写示例
你用什么浏览器测试就下载什么浏览器驱动
下面先说一下自动化环境常见问题
Firefox
1. Firefox路径问题
firefox火狐浏览器去完成自动化测试时,代码报了如下错误:
Cannot find firefox binary in PATH. mark sure firefox is installed
错误原因:firefox安装在其它路径,不是默认的安装路径
代码设置:
//换成你自己火狐浏览器安装位置
System.setProperty("webdriver.firefox.bin", "I:\\WebDriver\\Mozilla Firefox\\firefox.exe");
解决办法:指定firefox可执行文件路径:webdriver.firefox.bin
2. selenium 3.x Firefox驱动问题
使用selenium3.x+firefox火狐浏览器去完成自动化测试时,代码报了如下错误:
The path to the driver executable must be set by the webdriver.gecko.driver system properity
错误原因:
缺少火狐浏览器驱动包。如果selenium版本是3.x的,需要使用驱动包
代码设置:
System.setProperty("webdriver.gecko.driver", "src/resources/geckodriver.exe");
3. selenium与驱动版本匹配问题
使用selenium2.x版本+firefox去完成自动化测试时,代码报了如下错误:
Unable to connect to host 127.0.0.1 on port 7055 after 35000ms
错误总结:firefox浏览器版本和selenium版本不适配。
解决办法:建议降级火狐版本到47以下(比如:selenium 2.53.0+firefox 46)
4.selenium for firefox说明
selenium 2.x
selenium 2.x中自动集成了firefox驱动。
因此只须selenium 版本与firefox浏览器版本对应即可
selenium版本 | Firefox浏览器 |
---|---|
2.25.0 | v 18 |
2.30.0 | v 19 |
2.31.0 | v 20 |
2.42.2 | v 29 |
2.44.0 | v 33 (不支持31) |
2.52.0 | v 45.0 |
2.53.0 | v 46.0 |
2.53.1 | v 47.0.1 |
selenium 3.x
selenium,geckodriver,firefox 对应版本说明:
selenium版本 | geckodriver版本 | Firefox版本 |
---|---|---|
3.3 | 0.15 | v 48+ |
3.4 | 0.16 | v 52 |
3.4 | 0.17 | v 52 |
3.4 | 0.18 | v 53 |
3.5 | 0.19 | v 55 |
3.11 | 0.21 | v 57 |
从selenium 3.0.0开始就要求firefox为48及以上版本
selenium 3.x使用的java版本为jdk 1.8
selenium 3.x使用geckodriver作为firefox浏览器的驱动的替代
4、Firefox自动化相关工具链接
Change log :https://raw.githubusercontent.com/SeleniumHQ/selenium/master/java/CHANGELOG
Firefox驱动https://github.com/mozilla/geckodriver/releases/
Firefox各版本 http://ftp.mozilla.org/pub/firefox/releases/
Firefox驱动 镜像 https://npm.taobao.org/mirrors/geckodriver/
InternetExplorer
- IE浏览器驱动问题使用IE浏览器去完成自动化测试时,代码报了如下错误:
The path to the driver executable must be set by the webdriver.ie.driver system property
错误总结:缺少IE浏览器驱动包
解决办法:往项目中添加IE驱动包,并加载驱动的配置。
下载地址 http://www.seleniumhq.org/download/
建议下载版本:3.7.0:http://selenium-release.storage.googleapis.com/index.html?path=3.7/
IE驱动版本与Selenium版本保持相同即可:http://selenium-release.storage.googleapis.com/index.html
System.setProperty("webdriver.ie.driver", "src/resources/IEDriverServer.exe");
2. IE浏览器保护模式问题:使用IE浏览器去完成自动化测试时,代码报了如下错误:
Protected Mode Settings are not the same for all zones
解决方法1:
浏览器设置(但是换一台电脑就不适用了)
打开IE浏览器->工具->安全->全部勾选启用保护模式
浏览器设置(但是换一台电脑就不适用了)
打开IE浏览器->工具->安全->全部勾选启用保护模式
解决方法2:
忽略浏览器保护模式的设置InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS
代码:
//取消IE安全设置(忽略IE的Protected Mode的设置)
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
3. IE浏览器缩放设置
使用IE浏览器去完成自动化测试时,代码报了如下错误:
Browser zoom level was set to 125%.It should be set to 100%
错误总结:
浏览器缩放级别设置不对导致的(点工具栏页面->缩放设置)
解决办法:
忽略此设置:InternetExplorerDriver.IGNORE_ZOOM_SETTING
代码:
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
4. IE浏览器window丢失问题
使用IE浏览器去完成自动化测试时,代码报了如下错误:
unable to find element with id -- kw
错误总结:不是因为没有设置等待时间,而是因为之前的window对象已经丢失
解决办法:最快的解决办法是直接指定一个初始化页InternetExplorerDriver.INITIAL_BROWSER_URL
代码:
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, "http://www.baidu.com");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, "http://www.baidu.com");
Chrome
1. chrome浏览器驱动路径
使用Chrome做测试时,报了如下错误:
The path to the driver executable must be set by the webdirver.chrome.driver system properity
解决方案:系统设置Chrome驱动文件的路径
System.setProperty("webdriver.chrome.driver", "xxx");
2. chrome浏览器与chromeDriver匹配问题
使用chrome浏览器去完成自动化测试时,chrome浏览器停止运行chromedriver.exe 已停止工作
错误总结:chrome浏览器版本过高,虽然根据官网上的信息,2.33的chrome驱动支持60-62的谷歌。但是60根本不行
解决办法:降级chrome
3.Chrome与ChromeDriver版本对照表
ChromeDriver 版本 | 支持的 Chrome 版 |
---|---|
v2.41 | v67-69 |
v2.40 | v66-68 |
v2.38 | v65-67 |
v2.37 | v64-66 |
v2.36 | v65-67 |
v2.35 | v62-64 |
v2.34 | v61-63 |
v2.33 | v60-62 |
v2.32 | v59-61 |
v2.31 | v58-60 |
v2.30 | v58-60 |
v2.29 | v56-58 |
chrome浏览器各版本
http://www.chromedownloads.net/chrome64win/
禁止谷歌浏览器更新
https://jingyan.baidu.com/article/76a7e409f2137afc3b6e15be.html
ChromeDriver 镜像 http://npm.taobao.org/mirrors/chromedriver
Selenium 镜像 http://npm.taobao.org/mirrors/selenium
JDK版本问题
使用3.x的selenium来完成自动化测试时,代码报了如下错误:
Exception in thread "main" java.lang.UnsupportedClassVersionError:
错误总结:
3.x的selenium需要1.8的jdk,可能jdk版本过低
解决办法:
降级selenium版本,或提高jdk的版本为1.8
下面是chrome浏览器使用的示例代码:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
package com.baojiaren;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.File;
import java.util.concurrent.TimeUnit;
/**
* @author ✎ℳ๓₯㎕.倾心❀、
* @create 2021-05-15-15:20
*/
public class Main {
public static void main(String[] args) throws InterruptedException {
// 让系统知道chrome driver 的位置
// key 必须为webdriver.chrome.driver 否则报错
//等待分别有3种 需要等待的原因是页面元素还没加载完全程序运行太快获取不了元素 需要等元素加载完毕
//硬性等待 Thread.sleep(2000); 主线程直接休眠
// 隐式等待 wd.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
//WebDriverWait wait = new WebDriverWait(wd, 60);
/**
显示等待 时间单位为秒
WebDriverWait wait = new WebDriverWait(wd, 60);
页面元素存在并且可见
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("s_ipt")));
*/
System.setProperty("webdriver.chrome.driver", "I:\\WebDriver\\chromedriver.exe");
WebDriver wd = new ChromeDriver();
//tencent(wd);
// nav(wd);
//waits(wd);
//showWait(wd);
alerts(wd);
}
/**
* 显示等待
*
* @param wd
*/
public static void showWait(WebDriver wd) {
WebDriverWait wait = new WebDriverWait(wd, 60);
wd.manage().window().maximize(); //页面最大化
wd.get("http://www.baidu.com/");
// 页面元素存在并且可见
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("s_ipt")));
wd.findElement(By.className("s_ipt")).sendKeys("腾讯课堂");
wd.findElement(By.xpath("//*[@id=\"su\"]")).click();
// 页面元素是否在页面上可用和可被单击
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[text()='官方']")));
wd.findElement(By.xpath("//a[text()='官方']")).click();
ExpectedConditions.elementToBeClickable(By.xpath("//a[text()='官方']"));
Alert alert = wd.switchTo().alert();
alert.accept();
alert.dismiss();
alert.getText();
/*
其他的一些方法:
页面元素存在并且可见
visibilityOfElementLocated
页面元素是否在页面上可用和可被单击
elementToBeClickable()
页面元素是否被选中的状态
ExpectedConditions.elementToBeSelected(By.xpath("//a[text()='官方']"));
// 在页面元素中是否包含特定的文本
textToBePresentInElement()
//页面元素在页面中存在
presenceOfElementLocated()
找到alert弹框
wd.switchTo().alert()
Alert alert = wd.switchTo().alert();
alert.accept(); 确认
alert.dismiss();取消
alert.getText();获取文本
*/
}
public static void alerts(WebDriver wd) throws InterruptedException {
wd.manage().window().maximize();
wd.get("F:\\java_senior\\Selenium\\src\\main\\resources\\index.html");
Thread.sleep(2000);
wd.findElement(By.id("btn")).click();
Thread.sleep(2000);
Alert alert = wd.switchTo().alert();
System.out.println(alert.getText());
alert.accept();
}
public static void waits(WebDriver wd) {
wd.manage().window().maximize(); //页面最大化
wd.findElement(By.className("s_ipt")).sendKeys("腾讯课堂");
wd.findElement(By.xpath("//*[@id=\"su\"]")).click();
wd.findElement(By.xpath("//a[text()='官方']")).click();
}
/**
* 隐式等待
*
* @param wd
* @throws InterruptedException
*/
public static void tencent(WebDriver wd) throws InterruptedException {
// 隐式等待
wd.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
wd.get("http://www.baidu.com/");
WebDriver.Options op = wd.manage();
op.window().maximize(); //页面最大化
Thread.sleep(2000);
wd.get("http://www.baidu.com/");
wd.findElement(By.className("s_ipt")).sendKeys("腾讯课堂");
//点击百度按钮
Thread.sleep(2000);
wd.findElement(By.xpath("//*[@id=\"su\"]")).click();
Thread.sleep(2000);
System.out.println("==================" + wd.getWindowHandle());
System.out.println("所有==================" + wd.getWindowHandles());
wd.findElement(By.xpath("//a[text()='官方']")).click();
Thread.sleep(5000);
System.out.println("==================" + wd.getWindowHandle());
System.out.println("最后所有==================" + wd.getWindowHandles());
Dimension size = op.window().getSize();
int height = size.getHeight();
int width = size.getWidth();
System.out.println("窗口宽度" + width);
System.out.println("窗口高度" + height);
Point point = op.window().getPosition();
System.out.println(point.getX());
}
/**
* navigate对象操作
*
* @param wd
*/
public static void nav(WebDriver wd) {
WebDriver.Navigation navigate = wd.navigate();
//访问
navigate.to("http://www.jd.com");
//刷新
navigate.refresh();
//后退
navigate.back();
//前进
navigate.forward();
}
public static void bjTest(WebDriver wd) throws InterruptedException {
Thread.sleep(1000);
wd.manage().window().maximize(); //页面最大化
wd.get("http://baidu.com/");
wd.findElement(By.className("s_ipt")).sendKeys("宝家人");
Thread.sleep(1000);
//点击百度按钮
wd.findElement(By.xpath("//*[@id=\"su\"]")).click();
Thread.sleep(1000);
wd.findElement(By.xpath("//*[@id=\"1\"]/h3/a")).click();
Thread.sleep(5000);
Thread.sleep(5000);
//wd.findElement(By.cssSelector("a[title='海带']")).click();
System.out.println("==================" + wd.getWindowHandle());
}
public static void bjrTest(WebDriver wd) throws InterruptedException {
wd.get("http://baidu.com/");
/* try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
//wd.findElement(By.name("wd")).sendKeys("百度");
// wd.findElement(By.linkText("新闻")).click();
//wd.findElement(By.partialLinkText("hao")).click();
wd.findElement(By.className("s_ipt")).sendKeys("宝家人");
//点击百度按钮
wd.findElement(By.xpath("//*[@id=\"su\"]")).click();
//wd.findElement(By.xpath("//input[@name=\"wd\"]")).sendKeys("宝家人");
//wd.findElement(By.xpath("//input[@name=\"wd\"]"));
wd.findElement(By.id("su")).click();
Thread.sleep(1000);
//wd.findElement(By.xpath("//*[@id=\"1\"]/h3/a")).click();
wd.manage().window().maximize(); //页面最大化
wd.findElement(By.xpath("/html/body/div[8]/div/div[2]/div[2]/ul/li[1]")).click();
wd.findElement(By.cssSelector("a[title='海带']")).click();
System.out.println(wd.getCurrentUrl());
System.out.println(wd.getTitle());
System.out.println(wd.getPageSource());
// wd.quit();
}
}