doneright
doneright

Reputation: 111

Trying to create XPath from this HTML snippet

I have played for a while writing XPath but am unable to come up with exactly what I want.

I'm trying to write XPath for link(click1 and click2 in code snippet below) based on known text(myidentity in code snippet below). Can someone take a look into and suggest possible solution?

HTML code snippet:

<div class="abc">
  <a onclick="mycontroller.goto('xx','yy'); return false;" href="#">
    <img src="images/controls/inheritance.gif"/>
  </a>
  myidentity
  <span>
    <a onclick="mycontroller.goto('xx','yy'); return false;" href="#">click1</a>
    <a onclick="mycontroller.goto('xx','yy'); return false;" href="#">click2</a>
  </span>
</div>

Upvotes: 2

Views: 1362

Answers (3)

s_hewitt
s_hewitt

Reputation: 4302

See Macro's answer - this form should be used.

//div[text()[contains(., "myidentity")]]/span/a[2]

The following only works with one section of text in the containing div.

You'll need to select based on the text containing your identity text.

Xpath for click1

//div[contains(text(),"myidentity")]/span/a[1]

Xpath for click2

//div[contains(text(),"myidentity")]/span/a[2]

Upvotes: 0

Macros
Macros

Reputation: 7119

Hard to say without seeing the rest of the HTML but the following should work:

//div[text()[contains(., "myidentity")]]/span/a

Upvotes: 2

Dave Hunt
Dave Hunt

Reputation: 8223

You don't need to use XPath here, you could use a CSS locator. These are often faster and more compatible across different browsers.

css=div:contains(myidentity) > span a:nth-child(1) //click1
css=div:contains(myidentity) > span a:nth-child(2) //click2

Note that the > is only required to workaround a bug in the CSS locator library used by Selenium.

Upvotes: 2

Related Questions