WebDriver(Selenium2) 常见异常及处理方法

发表于:2014-12-29 13:00

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:Wind_365    来源:51Testing软件测试网采编

  对应的html为:
<a class="PageLink" title="2" href="/shanghai?pageno=2">2</a>
<a class="PageLink" title="3" href="/shanghai?pageno=3">3</a>
<a class="PageLink" title="4" href="/shanghai?pageno=4">4</a>
<a class="PageLink" title="5" href="/shanghai?pageno=5">5</a>
<a class="PageLink" title="6" href="/shanghai?pageno=6">6</a>
<a class="PageLink" title="7" href="/shanghai?pageno=7">7</a>
<a class="PageLink" title="8" href="/shanghai?pageno=8">8</a>
<a class="PageLink" title="9" href="/shanghai?pageno=9">9</a>
<a class="PageLink" title="10" href="/shanghai?pageno=10">10</a>
  原本在构造函数中通过driver.findElements(By.xpath("//a[@class = 'PageLink')来存储所有的PageLink到一个List<WebElement>中,然后通过Iterator.Next()的方式遍历所有PageLink页。
  但在第三页就出现异常:提示
  org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
  因此可以看出,在经过一次点击后,原有PageLink已经失效。需要重新获取。
  其原因在于,点击过一次PageLink后,会重新刷新并生成新PageLink,当前页的PageLink不会显示。
  用Selenium <wbr>WebDriver获取WebElement时的元素过期问题
  因此解决方法是设定了两个参数
  private Integer currentPageLinkNumber = 1;
  private Integer MaxPage = 10;//Max page links number
  然后通过
while(currentPageLinkNumber<MaxPage)
{
WebElement PageLink;
PageLink = driver.findElement(By.xpath("//a[@class = 'PageLink' and @title ='"+Integer.toString(currentPageLinkNumber+1)+"']"));
PageLink.click();
currentPageLinkNumber++;
//OtherOperation();
}
  的方式进行迭代。
  虽然感觉很麻烦就是- -
  要很小心的注意同步currentPageLinkNumber和当前的PageLink
22/2<12
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号