如何在Selenium Web驱动程序Java中按文本单击链接 [英] How to click a link by text in Selenium web driver java
问题描述
我的页面上有很多链接.一个链接带有文本"Add policyhoder",一个带有仅"Add"文本,另一个带有"Add PP".我需要按文本单击链接.我正在使用下面的代码单击仅具有添加"文本的链接,但是它正在单击文本中具有添加"的第一个链接,即屏幕上显示添加PP".帮助
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"));
我的要求是单击具有完全文本匹配的链接.例如在此处添加"
<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
谢谢 开发人员
推荐答案
如果有2个元素带有单词"Add",则尝试执行以下操作:
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();
要通过搜索确切的文本来查找元素,然后使用xpath会更有帮助.
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();
这篇关于如何在Selenium Web驱动程序Java中按文本单击链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!