簡體   English   中英

如何使用硒2定位元素,其中沒有唯一的標識符,例如名稱,ID

[英]How can I locate an element using selenium 2 which do not have unique identifiers like Name, Id

發起自動預呼(收貨人)發起自動預呼(收貨人)記錄手動預呼(收貨人)記錄手動預呼(收貨人)

據我了解,您想通過其文本內容來撤消Web元素。 由於CSS不允許您執行基於文本的搜索,因此xpath是您的唯一選擇。 嘗試這個:

WebElement webElement = driver.findElement(By.xpath("//li[a[text() = 'Initiate Auto Precall (Consignee)']]"));

根據注釋中給定的代碼,找到了您列出的用於查找元素的解決方案。

解決方案1:

使用元素列表並獲取它。

 List<WebElement> allOptions = driver.findElements(By.className("ui-corner-all"));

現在按索引獲取元素。

對於啟動自動預呼(Shipper) allOptions.get(0);

對於啟動自動預呼(收貨人) allOptions.get(1);

對於Log Manual Precall(Shipper) allOptions.get(2);

對於日志手動預呼(收貨人) allOptions.get(3);

解決方案2:

使用xpath如下:

用於啟動自動預呼(Shipper) (//a[@class='ui-corner-all'])[1]

對於啟動自動預呼(收貨人) (//a[@class='ui-corner-all'])[2]

對於日志手動預調用(Shipper) (//a[@class='ui-corner-all'])[3]

對於日志手動預呼(收貨人) (//a[@class='ui-corner-all'])[4]

driver.findElement(By.xpath("<xpath>"));

解決方案3:

在xpath中使用text()和contains()函數。

用於啟動自動預呼(Shipper)

//a[contains(text(),'Initiate') and contains(text(),'Shipper')]

發起自動預呼(收貨人)

//a[contains(text(),'Initiate') and contains(text(),'Consignee')]

對於日志手動調用(Shipper)

//a[contains(text(),'Log') and contains(text(),'Shipper')]

對於日志手動預呼(收貨人)

//a[contains(text(),'Log') and contains(text(),'Consignee')]

driver.findElement(By.xpath("<xpath>"));

希望您找到解決方案。

driver.findElement(By.cssSelector("<CSSSelectorHere>")).click();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM