4

I have many links in my page.One with text "Add policyhoder" ,one with text " Add" only and another "Add PP". I need to click link by text .I am using below code to click link having text as "Add" only but it is clicking very first link having "Add" in its text i.e. ""Add PP" available on screen.Please can u help

Driver.findElement(By.linkText("Add"));

My requirement is to click a link with exact text match . for example "Add" here

<td width="100%" colspan="7">
<table width="100%" cellspacing="0" cellpadding="1" valign="bottom">
<input id="hidPoClaim" type="hidden" onblur="ResetScript(this);" onfocus="HighlightScript(this);" value=" PolicySummary " callfunction="" size="" name="/Root/ACORD/InsuranceSvcRs/com.c_HomePolicyInquiryRs/co.cc_AvailableFunctions[com._FunctionName='PoSummary' and com.csc_FunctionName[@Action='ShowPolicyClaims']]">
<tbody>
<tr>
<td width="25%" valign="bottom" colspan="1">
<strong>
<font class="flabel">Policy Claims History:</font>
</strong>
</td>
<td width="20%" valign="bottom" colspan="1">
<font class="flabel">  </font>
<a class="fLabel" onclick="INFCaptureControlID(this); DoLink('POLICYLOSS','','ADD' );return false; " onblur="ResetScript(this);return true;" onfocus="HighlightScript(this);" delimiter="|" screenaction="ADD" href="" screen="Y" objecttype="POLICYLOSS" type="Link" context="Screen">**Add**</a>
</td>
<td width="20%" valign="bottom" colspan="1">
<td align="Center" width="15%" valign="bottom" colspan="1">
<td width="20%" colspan="1">
</tr>
</tbody>
</table

Thanks Dev

14
  • you are on right track , your way is fine to find element. just need to see if link is opening in same tab or new tab? Commented Nov 6, 2015 at 4:19
  • Opening in same tab. driver.findElement(By.xpath("//a[text()='Add']")); is also clicking very first link having "Add" in its text Commented Nov 6, 2015 at 10:11
  • yes so my answer will work for you , please see my given answer. Commented Nov 6, 2015 at 10:11
  • No i dont have to click any link before "Add" link. I just have to click one link having exact text as "Add" Commented Nov 6, 2015 at 10:19
  • then it should work :Driver.findElement(By.linkText("Add")); , can you please share website url? where you are trying? Commented Nov 6, 2015 at 10:24

3 Answers 3

3

If it the there are 2 elements with the word "Add", Then try something like this:

List<WebElement> list = driver.findElements(By.linkText("Add"));
list.get(1).click();

To find the element by searching for the exact text, then using xpath will be more helpful.

// For "Add" link, according to the HTML you've added to the question
driver.findElement(By.xpath("//a[text()='**Add**']")).click();
Sign up to request clarification or add additional context in comments.

7 Comments

What if there are multiple links with texts Add,Add JP,Add VP etc. is there any way to click a link with exact text match?.
@DevkantKrishnatrey - Then try using xpath driver.findElement(By.xpath("//*[text()='Add']"));
you need list element only if you have multiple links with specific text "Add". if you have text like "Add JP" then you can manage with single findelement as you did.
@DevkantKrishnatrey - I've update my answer. Let me know if you still face an issue.
@DevkantKrishnatrey - Check the HTML code: ... context="Screen">**Add**</a> ... The text is **Add** and not just Add. You have to modify the xpath to exact value you want it to find. Try driver.findElement(By.xpath("//a[text()='**Add**']")).click
|
0

I assume because when you click on first link it opens that link page in same tab and can not find second link element as page changed , so you have to open that link page in new tab in.

I think your code would be :

String Newtab = Keys.chord(Keys.CONTROL,Keys.RETURN); 
driver.findElement(By.linkText("Add policyhoder")).sendKeys(Newtab);

driver.findElement(By.linkText("Add")).click(); 

Now above code will work for you.

Comments

-1

you can try using

driver.findElement(By.xpath("//a[text()='Add']"));

This would match exact text for you

1 Comment

i am using above code as suggested It is still clicking very first one "Add....." not required "Add" link so its not searching for the exact text,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.