在selenuim中执行Mousehover操作的NoSuchElementException

时间:2016-05-06 10:14:20

标签: selenium selenium-webdriver

我有一个网页,我正在尝试执行鼠标悬停..有一个名为“实体记录”的元素,如果你将鼠标悬停在它上面,它会显示一个菜单。在该菜单中,我需要单击名为Create New record的元素。

我尝试过3种不同的代码,但它对我不起作用..

第一名:

 WebElement el = driver.findElement(By.xpath("\\...."));
    action.moveToElement(el).build().perform();
    Actions builder = new Actions(driver);
    builder.moveToElement(driver.findElement(By.cssSelector("..."))).click().build().perform();

第二个:

WebElement entityrecordsmenu = driver.findElement(By.cssSelector("..."));
    Actions builder = new Actions(driver);
    builder.moveToElement(entityrecordsmenu).build().perform();
    WebElement createnewrecord = driver.findElement(By.cssSelector("..."));
    createnewrecord.click();

第3名:

Actions action2 = new Actions(driver);
    WebElement ele = driver.findElement(By.xpath("...));
    action2.moveToElement(ele).build().perform();
    Actions build2 = new Actions(driver);
    build2.moveToElement(driver.findElement(By.xpath("..."))).click().build().perform();

第一个和第二个代码正在使鼠标悬停正常,但它没有点击菜单元素。有人能建议我这样做的正确方法吗?

1 个答案:

答案 0 :(得分:0)

鼠标悬停后,尝试等待显示该元素,然后单击。

 WebElement entityrecordsmenu = driver.findElement(By.cssSelector("..."));
    Actions builder = new Actions(driver);
    builder.moveToElement(entityrecordsmenu).build().perform();
    //provide wait here to display dropdown
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    //some time implicity wait may fails then use thread.sleep
    //Thread.sleep(3000);
    WebElement createnewrecord = driver.findElement(By.cssSelector("..."));
    createnewrecord.click();

    //if still above once does  not work you can try
    Actions builder1 = new Actions(driver);
    builder1.moveToElement(entityrecordsmenu).click(driver.findElement(By.cssSelector("..."))).build().perform();

谢谢你, 穆拉利