如何使用Java在Selenium WebDriver中执行鼠标悬停功能?

pra*_*ter 124 java selenium mouseover selenium-webdriver

我想在下拉菜单中执行鼠标悬停功能.当我们将鼠标悬停在菜单上时,它会显示新选项.我尝试使用xpath单击新选项.但无法直接单击菜单.所以,作为我试图将鼠标悬停在下拉菜单上的手动方式,然后将单击新选项.

Actions action = new Actions(webdriver);
WebElement we = webdriver.findElement(By.xpath("//html/body/div[13]/ul/li[4]/a"));
action.moveToElement(we).build().perform();
Run Code Online (Sandbox Code Playgroud)

Mar*_*nds 108

它实际上不可能执行"鼠标悬停"操作,而是需要将您想要一次完成的所有操作链接起来.因此,移动到显示其他元素的元素,然后在同一个链中,移动到现在显示的元素并单击它.

使用Action Chains时,你必须记住"像用户那样做".

Actions action = new Actions(webdriver);
WebElement we = webdriver.findElement(By.xpath("html/body/div[13]/ul/li[4]/a"));
action.moveToElement(we).moveToElement(webdriver.findElement(By.xpath("/expression-here"))).click().build().perform();
Run Code Online (Sandbox Code Playgroud)

  • 这不太合理的原因是所有对`webdriver.findElement(By ... something)`的调用都在其他任何事情之前执行(这是他们的结果可以传递给`moveElement`的唯一方法).那时你想要找到的第二个元素还不可见,因为第一个元素仍然必须悬停在上面.要解决这个问题,正如你所说,你可以插入中间的`.perform()`s,然后对于第二个`findElement`,第一个悬停将是`perform`ed.给定的解决方案可能会起作用,具体取决于页面的实现,但显然您和我的里程会有所不同. (8认同)
  • 对我来说,这不起作用.如果我在moveToElement()之后执行build().perform(),我的菜单只会被悬停 (5认同)

zmo*_*ris 52

尝试执行以下操作时,这些答案都不起作用:

  1. 将鼠标悬停在菜单项上.
  2. 找到悬停后仅可用的隐藏元素.
  3. 单击子菜单项.

如果在moveToElement之后插入'perform'命令,它会移动到元素,子菜单项会显示一段时间,但这不是悬停.隐藏元素会在找到之前立即消失,从而导致ElementNotFoundException.我尝试了两件事:

Actions builder = new Actions(driver);
builder.moveToElement(hoverElement).perform();
builder.moveToElement(clickElement).click().perform();
Run Code Online (Sandbox Code Playgroud)

这不适合我.以下对我有用:

Actions builder = new Actions(driver);
builder.moveToElement(hoverElement).perform();
By locator = By.id("clickElementID");
driver.click(locator);
Run Code Online (Sandbox Code Playgroud)

使用操作悬停和标准WebDriver单击,我可以悬停然后单击.

  • 添加.perform()时,第二个示例也适用于我 (2认同)

Zug*_*alt 24

根据这篇博客文章,我能够使用以下代码与Selenium 2 Webdriver触发悬停:

String javaScript = "var evObj = document.createEvent('MouseEvents');" +
                    "evObj.initMouseEvent(\"mouseover\",true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);" +
                    "arguments[0].dispatchEvent(evObj);";


((JavascriptExecutor)driver).executeScript(javaScript, webElement);
Run Code Online (Sandbox Code Playgroud)

  • 不太明显的解决方案,但我的 IE11 测试 100% 可靠。如果您在使用 `moveToElement` 悬停时遇到问题,请使用这个!我用 C# 编写代码,所以这不仅仅是 Java 的方式。 (2认同)

小智 11

此代码非常有效:

 Actions builder = new Actions(driver);
 WebElement element = driver.findElement(By.linkText("Put your text here"));
 builder.moveToElement(element).build().perform();
Run Code Online (Sandbox Code Playgroud)

鼠标悬停后,您可以继续在显示的信息上执行下一个操作


vin*_*ins 7

检查此示例我们如何实现此功能.

在此输入图像描述

public class HoverableDropdownTest {

    private WebDriver driver;
    private Actions action;

    Consumer < By > hover = (By by) - > {
        action.moveToElement(driver.findElement(by))
              .perform();
    };

    @Test
    public void hoverTest() {
        driver.get("https://www.bootply.com/render/6FC76YQ4Nh");

        hover.accept(By.linkText("Dropdown"));
        hover.accept(By.linkText("Dropdown Link 5"));
        hover.accept(By.linkText("Dropdown Submenu Link 5.4"));
        hover.accept(By.linkText("Dropdown Submenu Link 5.4.1"));
    }

    @BeforeTest
    public void setupDriver() {
        driver = new FirefoxDriver();
        action = new Actions(driver);
    }

    @AfterTest
    public void teardownDriver() {
        driver.quit();
    }

}
Run Code Online (Sandbox Code Playgroud)

有关详细解答,请点击此处 - http://www.testautomationguru.com/selenium-webdriver-automating-hoverable-multilevel-dropdowns/


Pla*_*ato 5

我发现这个问题正在寻找一种方法来为我的Javascript测试做同样的事情,使用Protractor(一个jlen前端到Selenium.)

我用量角器1.2.0和webdriver 2.1的解决方案:

browser.actions()
.mouseMove(
  element(by.css('.material-dialog-container'))
)
.click()
.perform();
Run Code Online (Sandbox Code Playgroud)

这也接受一个偏移量(我用它点击元素的上方和左侧:)

browser.actions()
.mouseMove(
  element(by.css('.material-dialog-container'))
  , -20, -20  // pixel offset from top left
)
.click()
.perform();
Run Code Online (Sandbox Code Playgroud)