[英]Selenium: How to click in a link image inside webtable?
I have the following table in my page (.html): 我的页面(.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>
I need to test this page: the click in the "exit.png" image (that's a link). 我需要测试此页面:单击“ exit.png”图像(这是链接)。 I write the following piece of code (in a for statement, from i=0 to 2), but it is not working: 我编写了以下代码(在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...
}
The result is the following error: 结果是以下错误:
org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document org.openqa.selenium.StaleElementReferenceException:陈旧元素引用:元素未附加到页面文档
I used //a/img[contains(@src,'exit.png')] instead of //img[contains(@src,'exit.png')] , but it doesn't work any away. 我使用了//a/img[contains(@src,'exit.png')]而不是//img[contains(@src,'exit.png')] ,但它并没有起作用。
How can I fix this problem? 我该如何解决这个问题?
The issue is that when you click a link, the collection of links in imgLinks
immediately becomes stale. 问题是,当您单击链接时, imgLinks
的链接imgLinks
立即变得陈旧。 Going back to the page, and trying to interact with the imgLinks
list is going to result in a state element reference error. 返回页面,并尝试与imgLinks
列表进行交互将导致状态元素引用错误。
This approach should work for you. 这种方法应该为您工作。 It will get a new list each time you return to the page. 每次您返回页面时,它将获得一个新列表。
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.