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

Time:09-25

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.

CodePudding user response:

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

//div[@class='table-responsive']/table

CodePudding user response:

Below xPath returns all the row values of the table.

//div[@class='table-responsive']/table/tbody/tr

how to deal with such elements that its xpath differs from time to time?

If a xPath value partially dynamic I would suggest you to us contains method. For an example,

<input class = "User1235">

In above xPath the value 1235 is dynamic in this case you can create a xPath with contains

//input[contains(@class,'User')]
  • Related