Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to click a link by text in Selenium web driver java

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

like image 393
Devkant Krishnatrey Avatar asked Jul 14 '26 19:07

Devkant Krishnatrey


1 Answers

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();
like image 129
JRodDynamite Avatar answered Jul 17 '26 17:07

JRodDynamite