2015-04-23 53 views
0

我正在使用Selenium Webdiver。我的測試案例是。如何使用selenium Webdriver處理Javascript單擊?

  1. 登錄到網站。
  2. 點擊通知鏈接。

現在所面臨的問題,而在通知鏈接點擊,具有HTML代碼如下: - :

/*1*/ driver.findElement(By.xpath("//a[@class='mTxt']")).click(); 

/*2*/ driver.findElement(By.cssSelector("div[class='topIcon notify']")).click(); 

/*3*/ driver.findElement(By.linkText("Notifications")).click(); 

/*4*/ driver.findElement(By.xpath("//div[@class='pNotifyCont dspN']")).click(); 

/*5*/ Actions mouse=new Actions(driver); 
    WebElement element=driver.findElement(By.xpath("//div[@class='pNotifyCont dspN']")); 
    mouse.moveToElement(element).click().build().perform(); 

Error : Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//a[@class='mTxt']"} Command duration or timeout: 7.56 seconds

<ul class="rghtSec fr menu logged"><li><a href="javascript:;"> 
    <div class="topIcon notify"><span>&nbsp;</span></div> 
    <div class="mTxt">Notifications<span id="rJobCntr" class="rJobCntr"></span></div></a> 
    <div class="subMenu recommendTT"> 
    <ul> 
     <li><a target="_blank" class="blob" id="blobId" href="http://jobsearch.naukri.com/notifications"> 
Fetching jobs you may apply for</a></li> 
    </ul> 

我已經通過以下5種不同的方式嘗試

但這些方式都不能解決問題:(,誰能幫我解決這個問題嗎?

+0

您的鏈接是否存在於iframe中? –

+0

韓..但是..對不起..它不在iframe /框架集。所以我不能使用它。 –

+0

請嘗試driver.findElement(By.xpath(「// a // div [@ class ='mTxt']」))。click(); –

回答

0

請更換您xpath表達

driver.findElement(By.xpath("//div[contains(text(),'Notifications')]")).click(); 

Notificationsdiv標籤。

如果你要選擇在這個Notifications鏈接的元素,您可以通過以下XPath:

driver.findElement(By.xpath("//div[contains(text(),'Notifications')]/following::a[1]")).click(); 
0

您可以直接點擊鏈接基於href屬性的值:

driver.findElement(By.cssSelector("a[href*='notifications']")).click(); 

driver.findElement(By.cssSelector("a[href=\"http://jobsearch.naukri.com/notifications\"]")).click(); 
相關問題