简体   繁体   English

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

[英]How to perform mouseover function in Selenium WebDriver using Java?

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.我尝试使用 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();

Its not really possible to perform a 'mouse hover' action, instead you need to chain all of the actions that you want to achieve in one go.执行“鼠标悬停”操作实际上是不可能的,相反,您需要将您想要一次性完成的所有操作串联起来。 So move to the element that reveals the others, then during the same chain, move to the now revealed element and click on it.因此,移动到显示其他元素的元素,然后在同一链中,移动到现在显示的元素并单击它。

When using Action Chains you have to remember to 'do it like a user would'.使用动作链时,您必须记住“像用户那样做”。

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();

None of these answers work when trying to do the following:尝试执行以下操作时,这些答案均无效:

  1. Hover over a menu item.将鼠标悬停在菜单项上。
  2. Find the hidden element that is ONLY available after the hover.找到仅在悬停后可用的隐藏元素。
  3. Click the sub-menu item.单击子菜单项。

If you insert a 'perform' command after the moveToElement, it moves to the element, and the sub-menu item shows for a brief period, but that is not a hover.如果在 moveToElement 之后插入“执行”命令,它会移动到该元素,并且子菜单项会显示一小段时间,但这不是悬停。 The hidden element immediately disappears before it can be found resulting in a ElementNotFoundException.隐藏元素在找到之前立即消失,导致 ElementNotFoundException。 I tried two things:我尝试了两件事:

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

This did not work for me.这对我不起作用。 The following worked for me:以下对我有用:

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

Using the Actions to hover and the standard WebDriver click, I could hover and then click.使用悬停操作和标准 WebDriver 单击,我可以悬停然后单击。

Based on this blog post I was able to trigger hovering using the following code with Selenium 2 Webdriver:基于这篇博文,我能够在 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);

This code works perfectly well:这段代码运行良好:

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

After the mouse over, you can then go on to perform the next action you want on the revealed information鼠标悬停后,您可以继续对显示的信息执行您想要的下一步操作

Check this example how we could implement this.检查这个例子我们如何实现这一点。

在此处输入图片说明

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();
    }

}

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

I found this question looking for a way to do the same thing for my Javascript tests, using Protractor (a javascript frontend to Selenium.)我发现这个问题正在寻找一种方法来为我的 Javascript 测试做同样的事情,使用量角器(Selenium 的 javascript 前端)。

My solution with protractor 1.2.0 and webdriver 2.1:我使用量角器 1.2.0 和 webdriver 2.1 的解决方案:

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

This also accepts an offset (i'm using it to click above and left of an element:)这也接受一个偏移量(我用它来点击元素的上方和左侧:)

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

Sample program to mouse hover using Selenium java WebDriver :使用 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();
    }
}

You can try:你可以试试:

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();

If you had case the web have many category, use the first method.如果您的网站有很多类别,请使用第一种方法。 For menu you wanted, you just need the second method.对于您想要的菜单,您只需要第二种方法。

I tried that and it worked normal我试过了,它工作正常

action = ActionChains(driver)
element = driver.find_element_by_xpath("XPath_selector")
action.move_to_element(element).perform()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM