nikhil
nikhil

Reputation: 3

in selenium web driver using eclipse how to clk on href link when two href with same text present in html code

I'm using selenium web driver and new to this tool. My html page contains two "href" tags with same text written in it, so i'm not able to locate element using findElement from eclipse and not able to click on href link using xpath method. Below is some portion of html

<li id="SubmodLI_113"> <a href="#" onclick="funSubModuleClick('../../Onboard/loadOnboardInboxMo‌​dule.do','111','113'‌​,'Inbox','-1','113')‌​">Inbox</a> </li>
<a href="#" onclick="funSubModuleClick('../../Onboard/loadOnboardInboxMo‌​dule.do','111','113'‌​,'Inbox','-1','113')‌​">Inbox</a> 
<ul class="pad15TLR">
   <li><i class="inboxic"></i></li>
   <li> <b>Inbox</b></li>
   <li><a href="#" onclick="fetchJob(1);">Inbox <span class="red"></span></a></li>
   <li></li>
</ul>
<a href="#" onclick="fetchJob(1);">Inbox <span class="red"></span></a>

Upvotes: 0

Views: 250

Answers (2)

alexey28
alexey28

Reputation: 5220

You can use position() to define element you need to click:

WebElement href1 = driver.findElements(By.xpath("//a[position()=1]"));
WebElement href2 = driver.findElements(By.xpath("//a[position()=2]"));

Upvotes: 0

Scornwell
Scornwell

Reputation: 605

It would be helpful if you provide the code you are looking at. Selenium allows you to select any part of the DOM when using findElement(), this means that the text can be exactly the same and you are still going to be able to select the hyperlink you need.

Based on your question i am going to have to assume the hyperlinks are not uniquely identified with an ID and also are under the same parent elements.

The key here is the XPath of that hyperlink. And finding how to write proper XPath can be a bit confusing for someone not familiar to it.

I use Firefox and the Firebug extension when working with selenium since it provides a very good XPath for any element seen on a web page.

The below is an example of XPath.

.//*[@id='osos-navbar-collapse']/div/nav/ul[2]/li[4]/a

This XPath says (Reading Right to left) "Select a hyperlink on item index 4, under Unorder List index 2, which is a child of a NAV tag and a child of a DIV tag under the parent with an ID of 'osos-navbar-collapse'

Upvotes: 0

Related Questions