我對硒很陌生。
我試圖找到按鈕驗證(見下文)而不是使用 xpath。
<div class="order">
<a style="float: left;" href="#143207" class="VoidDoc ui-button ui-corner-all ui-widget" role="button"> Void </a>
<a style="float: left;" href="validatedocumentation.php?documentId=143207&origin=viewreferral.php&OrderId=836829" class="ui-button ui-corner-all ui-widget" role="button"> Validate </a>
<a class="signDocBehalf ui-button ui-corner-all ui-widget" style="float: left;" href="#143207#20263#836829" role="button">Sign</a>
<a style="float: left;" href="editdocumentation.php?documentId=143207&origin=viewreferral.php" class="ui-button ui-corner-all ui-widget" role="button"> Edit </a>
</div>
我試圖通過類名(不幸的是不是唯一的)和按鈕的文本(“驗證”)來查看:
.....Driver.findElement(By.xpath("//a[@class='ui-button ui-corner-all ui-widget' and text()='Validate']")).click();
我也試過
.....Driver.findElement(By.xpath("//div[@class='ui-button ui-corner-all ui-widget' and text()='Validate']")).click();
得到一個錯誤:
no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class='ui-button ui-corner-all ui-widget' and text()='Validate']"}
(Session info: chrome=94.0.4606.81)
我猜是因為"<a>" is nested in "<div>",如果我錯了,請糾正我。
識別此按鈕如何正確。
請給我你的好建議。
uj5u.com熱心網友回復:
你幾乎是正確的。
您正在使用的 xpath
//a[@class='ui-button ui-corner-all ui-widget' and text()='Validate']
部分正確。文本 Validate有尾隨空格,因此您必須使用contains
使用這個 xpath :
//a[@class='ui-button ui-corner-all ui-widget' and contains(text(),'Validate')]
PS:請檢查dev tools(谷歌瀏覽器)我們是否有唯一的條目HTML DOM。
檢查步驟:
Press F12 in Chrome- >去element節- >做一個CTRL F- >再貼上xpath看看,如果你需要的element是越來越強調與1/1匹配的節點。
既然你提到你不想使用 xpath,不知道為什么?
如果是這種情況,請使用如下 css 選擇器:
a.ui-button.ui-corner-all.ui-widget[role='button'][href^='validatedocumentation.php?']
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/330729.html
上一篇:Selenium網格4:無法啟動新會話。可能的原因是遠程服務器地址無效或瀏覽器啟動失敗錯誤
下一篇:我如何從下一頁的表格中列出資訊
