How to perform mouseover function in Selenium WebD

2019-01-01 01:50发布

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

8条回答
与君花间醉酒
2楼-- · 2019-01-01 02:33

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();
查看更多
无色无味的生活
3楼-- · 2019-01-01 02:34

Check this example how we could implement this.

enter image description here

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

}

For detailed answer, check here - http://www.testautomationguru.com/selenium-webdriver-automating-hoverable-multilevel-dropdowns/

查看更多
荒废的爱情
4楼-- · 2019-01-01 02:35

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

查看更多
像晚风撩人
5楼-- · 2019-01-01 02:38

Sample program to mouse hover using 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();


}

}
查看更多
只靠听说
6楼-- · 2019-01-01 02:40

I found this question looking for a way to do the same thing for my Javascript tests, using Protractor (a javascript frontend to Selenium.)

My solution with protractor 1.2.0 and 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();
查看更多
姐姐魅力值爆表
7楼-- · 2019-01-01 02:48

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.

查看更多
登录 后发表回答