2015-11-06 38 views
0

我在我的頁面有很多鏈接。一個文本爲「Add policyhoder」,一個文本爲「Add」,另一個爲「添加PP」。我需要點擊鏈接的文本。我使用下面的代碼來點擊具有文本爲「添加」的鏈接,但它是點擊第一個鏈接有「添加」的文本,即「」添加PP「屏幕上可用。請你可以幫助如何點擊Selenium web驅動中的文本鏈接java

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 

感謝 開發

+0

你是在正確的軌道精確匹配的文本,用自己的方式是罰款尋找元素。只需要查看鏈接是否在同一個標​​籤或新標籤中打開? –

+0

在同一選項卡中打開。 driver.findElement(By.xpath( 「//一個[文本()= '添加']」));也點擊第一個鏈接的「添加」的文字 –

+0

是的,所以我的答案將適用於你,請看我給出的答案。 –

回答

0

如果它有2種元素單詞「Add」,然後tr y像這樣:

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

要通過搜索確切的文本來找到元素,那麼使用xpath會更有幫助。

// For "Add" link, according to the HTML you've added to the question 
driver.findElement(By.xpath("//a[text()='**Add**']")).click(); 
+0

如果有多個鏈接文本添加,添加JP,添加VP等有沒有什麼方法可以用精確的文字匹配點擊鏈接? –

+0

@DevkantKrishnatrey - 然後嘗試使用xpath'driver.findElement(By.xpath(「// * [text()='Add']」));' – JRodDynamite

+0

只有當您有多個鏈接與特定文本時,您才需要列表元素「加」。如果你有像「添加JP」這樣的文本,那麼你可以像使用單一的findelement一樣管理。 –

0

我假設,因爲當你點擊第一個鏈接它會打開在同一選項卡該鏈接頁面和頁面改變找不到第二個鏈接元素,所以你必須要打開新的標籤頁該鏈接頁面。

我覺得你的代碼將是:

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

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

現在,上面的代碼會爲你工作。

0

你可以嘗試使用

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

這會爲你

+0

我使用上面的代碼建議它仍然點擊第一個「添加.....」不需要「添加」鏈接,所以它不搜索確切的文字, –

相關問題