使用WebDriver在链接上单击事件在状态栏上显示“javascript:void(0)”

时间:2014-10-10 03:06:00

标签: java javascript selenium selenium-webdriver mousehover

我正在尝试点击鼠标悬停事件中生成的下拉列表中的第一个链接。

尝试了很多选择但没有成功,它只见过" javascript:void(0)"在状态栏上。到目前为止,鼠标悬停时显示下拉列表。只有点击第一个链接才能发出。

<a title="Insert" href="#">
    <span>Insert</span>
</a>
<ul>
     <li class="standardContent">
         <a id="id193" title="Modules & Topics from a Library" href="#"> Modules & Topics  from a Library </a>
     </li>
      <li class="customModule">

在这里,我想点击元素&#34;模块&amp;来自图书馆的主题&#34;。由于id是动态改变的。因此无法点击。

  Actions act = new Actions(driver);
        WebElement iconhover = driver.findElement(By.className("insertItems"));
        act.moveToElement(iconhover).click().build().perform();
        WebDriverWait wait = new WebDriverWait(driver,10);
        WebElement mod = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@class='standardContent']")));
        mod.click();

接下来尝试

  Actions act = new Actions(driver);
        WebElement iconhover = driver.findElement(By.className("insertItems"));
        act.moveToElement(iconhover).click().build().perform();
        WebDriverWait wait = new WebDriverWait(driver,10);
        WebElement mod = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@class='standardContent']")));
        WebElement a = driver.findElement(By.xpath("//*[@class='insertItems']/ul"));
        List <WebElement> ancartag= a.findElements(By.tagName("a"));
        for (int i=0; i<ancartag.size();i++){
            System.out.println(ancartag.get(0));
            ancartag.get(0).click();
        }

1 个答案:

答案 0 :(得分:0)

试试下面的代码:

WebElement element = driver.findElement(By.tagName("Modules & Topics  from a Library"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);