在调试代码的时候经常会遇到,selenium元素定位有时有效有时无效,先比较下
Selenium显示等待和隐式等待的区别
1、selenium的显示等待
原理:显示等待,就是明确的要等到某个元素的出现,等不到就一直等,除非在规定的时间之内都没找到,那么就跳出Exception
(简而言之,就是直到元素出现才去操作,如果超时则报异常)
2、selenium的隐式等待
原理:隐式等待,就是在创建driver时,为浏览器对象创建一个等待时间,这个方法是得不到某个元素就等待一段时间,直到拿到某个元素位置。
注意:在使用隐式等待的时候,实际上浏览器会在你自己设定的时间内部不断的刷新页面去寻找我们需要的元素
3、笨方法等待
使当前线程进入等待,Thread.sleep();这种等待属于死等,很容易让线程挂掉,使程序抛异常,所以要慎用此方法
综上使用显示等待的方式比较保险:
Java显式等待使用的是WebDriverWait+ExpectedConditions,使用方式如下:
new WebDriverWait(driver,10).until(
ExpectedConditions.presenceOfElementLocated(By.cssSelector("css locator")));
代码如下:
pom.xml 同上一篇配置,只需修改testng文件路径即可:
<suiteXmlFile>res/testng3.xml</suiteXmlFile>
testng.xml 文件如下:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite3">
<test name="百度搜索">
<classes>
<class name="com.test.practice.maventest.BaidDuSearch" />
<class name="com.test.practice.maventest.TestBaiDu" />
</classes>
</test>
</suite>
创建公共类BasePage
package com.test.practice.maventest;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
/**
* @author:lucy
* @date:2018年9月18日
* @Description: 处理页面元素公共类,重写页面操作事件,为每个元素加入显式等待
*/
public class BasePage {
WebDriver webDriver;
private final int timeOut = 10;
public BasePage(WebDriver webDriver) {
// TODO Auto-generated constructor stub
this.webDriver = webDriver;
}
public void sendkeys(WebElement wElement, String s) {
// 加入显式等待
new WebDriverWait(webDriver, timeOut).until(ExpectedConditions.visibilityOf(wElement));
wElement.clear();// 清空输入框
wElement.sendKeys(s);// 输入数据
}
public void click(WebElement wElement) {
// 加入显式等待
new WebDriverWait(webDriver, timeOut).until(ExpectedConditions.visibilityOf(wElement));
wElement.click();//
}
}
创建BaidDuSearch类
package com.test.practice.maventest;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
/**
* @author:lucy
* @date:2018年9月18日
* @Description: 百度页面,对象定位和操作,继承BasePage
*/
public class BaidDuSearch extends BasePage {
public BaidDuSearch(WebDriver webDriver) {
super(webDriver);
// TODO Auto-generated constructor stub
}
//定位输入框
@FindBy(id ="kw")
private WebElement kw_Element;
// 定位搜索按钮
@FindBy(id ="su")
private WebElement su_Element;
//输入关键字
public void kw_sendkeys(String s) {
this.sendkeys(kw_Element, s);
}
//点击搜索
public void su_click() {
this.click(su_Element);
}
}
创建测试类TestBaiDu,
package com.test.practice.maventest;
import org.testng.annotations.Test;
import junit.framework.Assert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
/**
* @author:lucy
* @date:2018年9月18日
* @Description: 测试百度搜索
*/
public class TestBaiDu {
private WebDriver driver;
@BeforeClass
public void beforeClass() {
System.setProperty("webdriver.firefox.marionette", "D:\\selenium\\geckodriver-v0.21.0-win64\\geckodriver.exe");
driver = new FirefoxDriver();
System.out.println("执行BeforeClass");
}
@Test
public void Baidu_Search() {
BaidDuSearch bSearch = PageFactory.initElements(driver, BaidDuSearch.class);
driver.get("https://www.baidu.com");
driver.manage().window().maximize();// 窗口最大化
bSearch.kw_sendkeys("软件测试");
bSearch.su_click();
}
@Test
public void f2() {
Assert.assertEquals("abc", "abc");
}
@AfterMethod
public void close() {
System.out.println("执行AfterMethod");
}
}
run as》maven test:
最后运行结果报告如下:
tips:
遇到问题,执行测试时,一直报
org.apache.maven.surefire.booter.SurefireExecutionException: Cannot instantiate class com.test.practice.maventest.TestBaiDu; nested exception is org.testng.TestNGException: Cannot instantiate class com.test.practice.maventest.TestBaiDu错误,通过重新初始化浏览器驱动,解决改该问题,代码如下:
System.setProperty("webdriver.firefox.marionette", "D:\\selenium\\geckodriver-v0.21.0-win64\\geckodriver.exe");