Search code examples
javaseleniumselenium-webdrivermouseovermousehover

Hover over on element and wait with Selenium WebDriver using Java


EDIT: So I figured out a simple way to hover over the element, but I want to wait for a result to pop up. The Chrome webdriver hovers over the element and moves on too fast for me to be able to see text. How can I get it to stay hovered until the text pops up? I looked at Wait() and until(), but I can't seem to get them to work properly (I assume that's because I'm not really waiting for a boolean to be true in the code. Unless someone has some suggestions?). Here's what I have thus far...

WebDriver driver = getWebDriver();
By by = By.xpath("//*[@pageid='" + menuItem + "']");
Actions action = new Actions(driver);
WebElement elem = driver.findElement(by);
action.moveToElement(elem);
action.perform();

Thanks again everyone!

Cheers.


Solution

  • Seems at the point I was at the method just was not waiting long enough for the text to become visible. Adding a simple sleep function to the end of it was exactly what I needed.

    @When("^I hover over menu item \"(.*)\"$")
    public void I_hover_over_menu_item(String menuItem)
    {
        WebDriver driver = getWebDriver();
        By by = By.xpath("//*[@pageid='" + menuItem + "']");
        Actions action = new Actions(driver);
        WebElement elem = driver.findElement(by);
        action.moveToElement(elem);
        action.perform();
        this.sleep(2);
    }
    
    public void sleep(int seconds) 
    {
        try {
            Thread.sleep(seconds * 1000);
        } catch (InterruptedException e) {
    
        }
    }
    

    Hope that helps others in a similar bind!

    Cheers!