如何使用SeleniumWebDriver处理Javascript点击?

如何使用SeleniumWebDriver处理Javascript点击?,javascript,selenium-webdriver,Javascript,Selenium Webdriver,我正在使用Selenium Webdiver。我的测试用例是 登录到网站 点击通知链接 我在单击通知链接时遇到问题,HTML代码如下:- <ul class="rghtSec fr menu logged"><li><a href="javascript:;"> <div class="topIcon notify"><span>&nbsp;</span></div> <div class=

我正在使用Selenium Webdiver。我的测试用例是

  • 登录到网站
  • 点击通知链接
  • 我在单击通知链接时遇到问题,HTML代码如下:-

    <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>
    
    错误:
    线程“main”org.openqa.selenium.NoSuchElementException中出现异常:找不到元素:{“method”:“xpath”,“selector”:”//a[@class='mTxt']”
    命令持续时间或超时:7.56秒


    但是这些方法都不能解决问题:(,有人能帮我解决这个问题吗?

    请将您的
    xpath
    表达式替换为

    driver.findElement(By.xpath("//div[contains(text(),'Notifications')]")).click();
    
    通知
    位于
    div
    标记中

    如果要选择此
    Notifications
    链接下的元素,可以通过以下xpath:

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

    您可以根据
    href
    属性的值直接单击链接:

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


    您的链接是否可能位于iframe?han..但是..抱歉..它不在iframe/frameset中。因此我无法使用它。请尝试driver.findelelement(By.xpath(“//a//div[@class='mTxt'])。单击();
    driver.findElement(By.cssSelector("a[href*='notifications']")).click();
    
    driver.findElement(By.cssSelector("a[href=\"http://jobsearch.naukri.com/notifications\"]")).click();