Bharat Nanwani
Bharat Nanwani

Reputation: 703

How can I select the 2nd td using Xpath?

Using below below I am able to locate the 1st td. However, I'm unable to locate the 2nd td using xpath? can you please help here?

Below code works correct for 1st TD -

WebElement SuperElement = (WebElement) jse.executeScript("return arguments[0].parentNode;", parentElement);
        return SuperElement.findElement(By.tagName("td")).getText();

Below code I need 2 td but doesn't work -

WebElement SuperElement = (WebElement) jse.executeScript("return arguments[0].parentNode;", parentElement);
        return SuperElement.findElement(By.xpath("/td[2]")).getText();

Upvotes: 0

Views: 327

Answers (1)

har07
har07

Reputation: 89325

Starting XPath with / always makes the XPath absolute i.e relative to the document node. You should start with ./ or just remove the / completely if you mean to get child element from current context element :

By.xpath("./td[2]")
By.xpath("td[2]")

Upvotes: 1

Related Questions