148

I want to do mouseover function over a drop down menu. When we hover over the menu, it will show the new options. I tried to click the new options using the xpath. But cannot click the menus directly. So, as the manual way I am trying to hover over the drop down menu and then will click the new options.

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

10 回答 10

119

它实际上不可能执行“鼠标悬停”动作,相反,您需要一次性链接您想要实现的所有动作。所以移动到显示其他元素的元素,然后在同一个链中,移动到现在显示的元素并单击它。

使用动作链时,您必须记住“像用户那样做”。

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();
于 2013-06-25T10:03:33.853 回答
61

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

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

如果在 moveToElement 之后插入“执行”命令,它会移动到元素,并且子菜单项会显示一小段时间,但这不是悬停。隐藏元素在被发现之前立即消失,从而导致 ElementNotFoundException。我尝试了两件事:

Actions builder = new Actions(driver);
builder.moveToElement(hoverElement).perform();
builder.moveToElement(clickElement).click().perform();

这对我不起作用。以下对我有用:

Actions builder = new Actions(driver);
builder.moveToElement(hoverElement).perform();
By locator = By.id("clickElementID");
driver.click(locator);

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

于 2014-07-28T19:39:34.533 回答
26

根据这篇博客文章,我能够使用 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);
于 2014-05-28T00:21:05.963 回答
11

这段代码运行良好:

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

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

于 2016-11-10T12:07:35.997 回答
7

检查这个例子我们如何实现它。

在此处输入图像描述

public class HoverableDropdownTest {

    private WebDriver driver;
    private Actions action;

    //Edit: there may have been a typo in the '- >' expression (I don't really want to add this comment but SO insist on ">6 chars edit"...
    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();
    }

}

有关详细答案,请在此处查看 - http://www.testautomationguru.com/selenium-webdriver-automating-hoverable-multilevel-dropdowns/

于 2017-05-22T14:54:49.390 回答
5

我发现这个问题正在寻找一种方法来为我的 Javascript 测试做同样的事情,使用量角器(Selenium 的 javascript 前端。)

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

browser.actions()
.mouseMove(
  element(by.css('.material-dialog-container'))
)
.click()
.perform();

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

browser.actions()
.mouseMove(
  element(by.css('.material-dialog-container'))
  , -20, -20  // pixel offset from top left
)
.click()
.perform();
于 2014-09-10T19:08:21.373 回答
4

使用 Selenium java WebDriver 鼠标悬停的示例程序:

public class Mhover {
    public static void main(String[] args){
       WebDriver driver = new FirefoxDriver();
       driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
       driver.get("http://www.google.com");
       WebElement ele = driver.findElement(By.id("gbqfba"));
       Actions action = new Actions(driver);
       action.moveToElement(ele).build().perform();
    }
}
于 2014-12-04T06:51:31.613 回答
3

你可以试试:

WebElement getmenu= driver.findElement(By.xpath("//*[@id='ui-id-2']/span[2]")); //xpath the parent

Actions act = new Actions(driver);
act.moveToElement(getmenu).perform();

Thread.sleep(3000);
WebElement clickElement= driver.findElement(By.linkText("Sofa L"));//xpath the child
act.moveToElement(clickElement).click().perform();

如果您的案例网络有很多类别,请使用第一种方法。对于您想要的菜单,您只需要第二种方法。

于 2017-12-07T08:33:45.177 回答
0

我试过了,它工作正常

action = ActionChains(driver)
element = driver.find_element_by_xpath("XPath_selector")
action.move_to_element(element).perform()
于 2021-06-10T09:14:27.247 回答
0

试试这个可重复使用的方法,

public void MoveThePoiterToElement(By by){
    log.info("Moving the cursor to the element");
    Actions action = new Actions(driver);
    action.moveToElement(driver.findElement(by));
    action.build().perform();
    log.info("Cursor moved to the element");
}
于 2021-09-17T08:25:04.217 回答