selenium java验证搜索结果遇到的问题
库\图片\1.png如图所示,想要遍历device type、device model、firmware name、firmware version这4列的值
代码如下:
@Test
public void test1() throws Exception {
String searchtext="gate";
driver.findElement(By.xpath("//input[@ng-model='search']")).clear();
driver.findElement(By.xpath("//input[@ng-model='search']")).sendKeys(searchtext);
driver.findElement(By.xpath("//button[@type='submit']")).click();
WebElement table = driver.findElement(By.tagName("table"));
int count = 0;
List<WebElement> rows = table.findElements(By.tagName("tr"));
for(WebElement row:rows){
List<WebElement> cols= row.findElements(By.tagName("td"));
for(WebElement col:cols){
Pattern pattern = Pattern.compile(searchtext,Pattern.CASE_INSENSITIVE);//大小写不敏感
Matcher matcher = pattern.matcher(col.getText());
if(matcher.find()){
System.out.println("第"+ row +"行:"+ col.getText());
count = count + 1;
break;
}
}
System.out.println("+++++++++++++++++++++");
}
AssertJUnit.assertEquals(count, rows.size());
}
按顺序遍历每行每列,当当前行匹配到第一个值符合搜索的字符时,该行剩下的列不再去匹配,跳到下一行,同时count+1(用来验证搜索到的总条目)
运行时出错了:
org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
百度了也不明白是怎么回事,是以为分页问题吗,求大神指教,不胜感激 顶 意思是元素过时了 代码没仔细看 是不是元素被刷新掉或者因为某些原因消失了
页:
[1]