简体   繁体   English

如何设置 Java Selenium 的 xPath 有两个相同的 ZA2F2ED4F8DCEBC2CBB4DZC21A2 表

[英]How to set xPath of Java Selenium when there are two table of same class

I trying to test this WEBSITE table data by using Java Selenium but there are two tables on the webpage with same class name datatable .我尝试使用 Java Selenium 测试此WEBSITE表数据,但网页上有两个表具有相同的 class 名称datatable When I set as:当我设置为:

private final static String table   = "//table[contains(@class,'datatable')]"; 

then its scraping first table on the page and once its given both tables data of first page.然后它在页面上抓取第一个表,一旦给定第一页的两个表数据。

How can I set xPath separately for each table with one class on the same page?如何在同一页面上为每个表分别设置 xPath 和一个 class?

You can use indexes like您可以使用索引,如

private final static String tablefirst   = "(//table[contains(@class,'datatable')])[1]"; 
private final static String tablelast   = "(//table[contains(@class,'datatable')])[last()]"; 

You can use below xpath as well.您也可以使用下面的 xpath。 more generic.更通用。

private final static String tablefirst   = "//p[text()='Found 1096 Airline Codes']/following::table[1]"; 
private final static String tablelast   = "//p[text()='Found 9130 Location Codes']/following::table[1]"; 

It is very easy if you know, and from your question I assume you know which is first and which is second table.如果您知道,这很容易,并且根据您的问题,我假设您知道哪个是第一张桌子,哪个是第二张桌子。 In that case use:在这种情况下使用:

driver.findelements(By.xpath("//table[contains(@class,'datatable')]")).get(index)

if you want first table如果你想要第一张桌子

index=0

if you want secondtable如果你想要第二张桌子

index=1

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

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