【问题标题】:Mouse hover and subsequent click on menu item鼠标悬停并随后单击菜单项
【发布时间】:2013-08-15 04:15:21
【问题描述】:

在无头模式下使用 selenium web-driver API,我试图在菜单项上执行鼠标悬停;以便显示所有子菜单项。之后,我单击其中一个子菜单项。以下是sn -p的代码:

Actions actions = new Actions(driver);
WebDriverWait wait = new WebDriverWait(driver, 20);

//Waiting for menu item to be clickable
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(menu_item)));
WebElement firstLink = driver.findElement(By.cssSelector(menu_item));
//Hovering over the menu item
actions.moveToElement(firstLink).build().perform();
//Waiting for the sub-menu item to be clickable - ERRORS OUT HERE
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(submenu_item)));
//Getting the second link
WebElement secondLink = driver.findElement(By.cssSelector(submenu_item));
//Click on sub menu
actions.moveToElement(secondLink).click().build().perform();

但是,测试超时,说找不到子菜单项。

我已验证上述操作与 Firefox 驱动程序完美兼容。 此外,css 选择器是正确的。

PhantomJs 和鼠标悬停操作是否存在任何已知问题 - 这可能解释了为什么代码不能使用 PhantomJs 驱动程序运行。如果是这样,是否有任何解决方法。

另外,是否有更好的方法来对 API 进行排序以模拟鼠标悬停然后单击操作?

(注意:我也尝试链接How to perform mouseover function in Selenium WebDriver using Java? 中提到的操作,但没有帮助)

【问题讨论】:

  • 我认为 PhantomJS 还不支持这个。您找到解决方法了吗?
  • 简单回答:Javascript 没有被执行!

标签: java selenium-webdriver phantomjs


【解决方案1】:

尝试使用非原生 JavascriptExecutor:

public void mouseHoverJScript(WebElement HoverElement) {
            String mouseOverScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover', true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}";
            ((JavascriptExecutor) driver).executeScript(mouseOverScript,
                    HoverElement);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 2023-03-31
    相关资源
    最近更新 更多