How to set xPath of Java Selenium when there are two table of same class Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to set xPath of Java Selenium when there are two table of same class without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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. 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?

Answer

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. 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]"; 
We are here to answer your question about How to set xPath of Java Selenium when there are two table of same class - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji