繁体   English   中英

Selenium:如何在webtable中单击链接图像?

[英]Selenium: How to click in a link image inside webtable?

我的页面(.html)中有下表:

<table>
 <thead>
  <td>Id</td>
  <td>Descricao</td>
  <td>Quant</td>
  <td>Preco</td>
  <td>Acoes Disponiveis</td>
 </thead>
 <tbody>
  <tr>
   <td>1</td>
   <td>Item Produto XMasInicial</td>
   <td>20</td>
   <td>2.30</td>
   <td>
    <a href="passoInicial.html">Cadastrar Outro</a> 
    <a href="passoInicial.html"><img src="exit.png" /></a>
   </td>
  </tr>
  <tr>
   <td>2</td>
   <td>Item Produto XMasFinal</td>
   <td>50</td>
   <td>1.90</td>
   <td>
    <a href="passoFinal.html">Ficar na mesma</a>
    <a href="passoFinal.html"><img src="exit.png" /></a>
   </td>
  </tr>
  <tr>
   <td>3</td>
   <td>Item Produto XMasGoogle</td>
   <td>50</td>
   <td>1.90</td>
   <td>
    <a href="paginaVazia.html">Ir para o Google</a> 
    <a href="paginaVazia.html"><img src="exit.png" /></a>
   </td>
  </tr>
 </tbody>
</table>

我需要测试此页面:单击“ exit.png”图像(这是链接)。 我编写了以下代码(在for语句中,从i = 0到2),但是它不起作用:

List<WebElement> imgLinks = this.webDriver.findElements(By.xpath("//img[contains(@src,'exit.png')]"));
for(...) {
imgLinks.get(i).click();
//some code to return for this html page (back) and click in the next image...
}

结果是以下错误:

org.openqa.selenium.StaleElementReferenceException:陈旧元素引用:元素未附加到页面文档

我使用了//a/img[contains(@src,'exit.png')]而不是//img[contains(@src,'exit.png')] ,但它并没有起作用。

我该如何解决这个问题?

问题是,当您单击链接时, imgLinks的链接imgLinks立即变得陈旧。 返回页面,并尝试与imgLinks列表进行交互将导致状态元素引用错误。

这种方法应该为您工作。 每次您返回页面时,它将获得一个新列表。

List<WebElement> imgLinks = this.webDriver.findElements(By.xpath("//img[contains(@src,'exit.png')]"));
int imgLinksLength = imgLinks.size();
for(int i = 0; i < imgLinksLength ; i++) {
    List<WebElement> imgLinks = this.webDriver.findElements(By.xpath("//img[contains(@src,'exit.png')]"));
    imgLinks.get(i).click();
    //some code to return for this html page (back) and click in the next image...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM