How to find the xpath of element of a web page that located by it's class name | Java selenuim


I have a table on a web page that its xpath differs from time to time but its class name doesn’t. so I can handle it by its class name but my code needs the xpath of that element so how to get the xpath of that element?
OR how to deal with such elements that its xpath differs from time to time?

    String xpath = "here sould be the xpath of that element";
    wait_page_loading(By.xpath(table_xpath)); //this other function in my class wait the page loading
    WebElement Webtable = driver.findElement(By.xpath(table_xpath));
    List<WebElement> totalRowCount = Webtable.findElements(By.xpath(table_xpath + "/tbody/tr"));
    if(totalRowCount.size() <= 1) {
        throw new Exception("Can't find results in the problem page");
    return totalRowCount.get(0).findElements(By.xpath("td")).get(6).getText();

Thanks a lot in advance.


I see a div and then we have a child table, please use the below xpath :


Answered By – cruisepandey

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published