4.3 Webdriver Actions

Various webpage actions like mouseHover, sliding element etc. can be performed using Webdriver.

Mouse Hover first approach

@Test
    public void webDriverActions() {
        driver.get("http://someURl.com");
        // Get element to be hovered upon
        WebElement hoverElement = driver.findElement(By.xpath("//div[@class='hoverElement']"));

        // Perform hover action
        Actions builder = new Actions(driver);
        builder.moveToElement(hoverElement).build().perform();

        // Click the element that is now visible after hover
        driver.findElement(By.xpath("//div[@class='visibleElementOnHover']")).click();
    }

Mouse Hover second approach

@Test
    public void webDriverMouseActions() {
        driver.get("http://someURl.com");
        // Get element to be hovered upon
        WebElement hoverElement = driver.findElement(By.xpath("//div[@class='hoverElement']"));

        // Perform hover action using Mouse object
        Locatable hoverItem = (Locatable) hoverElement;
        Mouse mouse = ((HasInputDevices) driver).getMouse();
        mouse.mouseMove(hoverItem.getCoordinates());

        // Click the element that is now visible after hover
        driver.findElement(By.xpath("//div[@class='visibleElementOnHover']"))
                .click();
    }

Move Slider to an offset

Last updated

Was this helpful?