Ron C
Ron C

Reputation: 1

Selenium returning text from path not specified in xpath statement

I have text located in these two locations:

The elements I want the text from:

//*[@id="RoleSetUpNewForm"]/div/table/tbody/tr[1]/td[2]/table[2]/tbody/tr/td/div[1]/table[3]/tbody/tr[2]/td/table[1]/tbody/tr/td[2]/table[1]/tbody/tr/td/label

The elements that I do not want the text from: //*[@id="home"]/div[16]/form/div/table/tbody/tr[1]/td[1]/table/tbody/tr[18]/td[3]/div/a

This xpath statement when executed in Selenium keeps giving me both the elements I want, AND the elements I do not want:

driver.findElements(By.xpath("//td[@class='level3TD']/label"));

Clearly the xpath above does not reference the location that I do not want the text from, but I am getting the text elements from it returned..

I am just baffled. Any ideas? Is there something I need to do with my selenium xpath query to stop the text from the path ending in /div/a from coming back?

Upvotes: 0

Views: 94

Answers (2)

Ron C
Ron C

Reputation: 1

I figured this out, there were duplicate label text on the page that made me think the xpath was pulling from the wrong location. Lets close this.

Upvotes: 0

RStevoUK
RStevoUK

Reputation: 456

There is no need to manually write XPath's.

Open the dev console by pressing F12 (assuming you are on a Windows machine).

Now find the node that you want, right click on it and click "Copy". Now you can copy the relative XPath, Full XPath, JS Path or Selector. In your case you look like you want the Full XPath.

This can be done on all machines, in all browsers.

Upvotes: 0

Related Questions