4

I want to click the link in one container but the container contains two links having the same identifier.

For example:

<div id="container" style="height:88 px; width: 310px;">
  <a class="instanceLink" href="/User/GoToInstance/747328">priyasoft tutorial</a>
  <a class="instanceLink" href="/User/GoToInstance/747685">priyasoft tutorial</a>
</div>

How can I specifically target one of them?

5

When there are multiple instances and there is no other way to distinguish them* you can refer to the instance index, i.e. 'which one'.

CSS

a.instanceLink:nth-child(1)

or

a.instanceLink:nth-child(2)

XPath

Note: 0 based index

a[@class='instanceLink'][0]

or

a[@class='instanceLink'][1]

You can see these in chrome tools, e.g.

css   - $('a.instanceLink:nth-child(1)')
xpath - $$('a[@class='instanceLink]')[0]

This is true for the example given. However this now creates a new problem. You now have locators that are tied to the layout of the page, i.e. that there must be two links, not one or three and the one you want is in a specific position. The makes the test more fragile and subject to being broken by page layout or content changes in the future. In other cases where there is a way to distinguish the links from other markup (but again not relying too heavily on layout).

For example for a page that is

<span id="header">
  <a href='x.html' >link</a>
</span>
<span id="footer">
  <a href='x.html' >link</a>
</span>

instead of using an index you may be better off using .header a or .footer a as they are unique and also have semantic meaning ("header") which [0] does not.

0

You can use either css selector or xpath

Css Selector:

  • First priyasoft tutorial element: div[id='container'] a[class='instanceLink'], this reads first find a div element that has id of container, then look for its first a child that has class as instanceLink.
  • Second priyasoft tutorial element: div[id='container'] a[class='instanceLink']+a, this reads, first find a div element that has id of container, then look for its first a child that has class as instanceLink finally look for its (first child) next a sibling.

Xpath:

  • First priyasoft tutorial element: //div[@id='container']/a[@class='instanceLink'], first find a div element that has id of container, then look for its first a child that has class of instanceLink OR //div[@id='container']/a[1], it reads first find a div element that has id of container, then look for its first a child.
  • Second priyasoft tutorial element: //div[@id='container']/a[2], it reads first find a div element that has id of container, then look for its second a child.
  • driver.navigate().to("insightly.com/"); Homepage.link_account(driver).click(); System.out.println("login"); Loginpage.username(driver).sendKeys("priyasaran97@gmail.com"); Loginpage.pwd(driver).sendKeys("saran123"); Loginpage.login(driver).click(); System.out.println("login ok"); Homepage.priyasoftlink(driver).click(); System.out.println("passed"); – jeni100 Dec 8 '16 at 4:57
  • Hi Zhang,Thankyou, i tried the possibility you mentioned.pls refer my above comment,there i have mentioned the code which i tried – jeni100 Dec 8 '16 at 4:58
  • Hi Zhang,Thankyou, i tried the possibility you mentioned. return element=driver.findElement(By.xpath("//div[@id='container']/a[1]")); pls refer my above comment,there i have mentioned the code which i tried – jeni100 Dec 8 '16 at 5:04
  • My result Zone also displayed as "INFO: Detected dialect: W3C login login ok passed" But actually the link is not been clicked, the screen retains the same page. but result showing passed. if we manually clicking the link is bringing us to next page.pls do help – jeni100 Dec 8 '16 at 5:05
0

You target a specific one by using a more specific selector. In this case, you should be able to use an elegant little CSS selector like this:

a.instanceLink[href*="747328"]

This approach of using a very specific selector also helps your test be more robust: when additional links are added before or after the one you're trying to select, the selector won't suddenly resolve to a different link, it still finds exactly the same one. This helps your test code remain maintainable.

0

You can use xpath to click on the specific link if you have the same identifier.

XPATH to click on 1st link : //div[@id='container']/a[1]

XPATH to click on 2nd link: //div[@id='container']/a[2]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.