[英]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.