解决webdriver(Element not found in the cache - perhaps the page has changed since it was looked up )

测试技术交流群:161204772

悦分享测试联盟:136924235

悦分享主页:http://www.bjhxcc.com/special/2013/0822/yfx.htm

解释:这种问题是由于,当我们执行某个ajax操作后,对于之前定位到的DOM对象与之发生变化,所以导致查找不到

解决思路:让driver查找DOM元素时不进行缓存查找,而是对当前页面进行查询

使用pageFactory类结合FindBy注释完成此操作

1、定义一个类,封装WebElement对象元素

class MyElement{

@FindBy(xpath="//input[@id='hello']")

public WebElement userName;

public WebElement password; //如果该元素存在id或name属性,直接使用id或name作为属性名,不必在使用@FindBy注解

public MyElement(WebDriver driver){

PageFactory.initElements(driver,this);

}

}

public class Test{

WebDriver driver = new FirefoxDriver();

MyElement my = new MyElement(driver);

my.userName.sendKeys("ray");

my.password.sendKeys("123456");

}


部分元素如果需要从cache中读取,只需在MyElement类中的属性加上@CacheLookUp注解即可,当然如果没有被@FindBy注解的属性,不必这样操作,默认会Cache查找

你可能感兴趣的:(解决webdriver(Element not found in the cache - perhaps the page has changed since it was looked up ))