Hover over on element and wait with Selenium WebDriver using Java Hover over on element and wait with Selenium WebDriver using Java selenium selenium

Hover over on element and wait with Selenium WebDriver using Java


You can't rely on sleeps so you should try this:

WebElement myDynamicElement = (new WebDriverWait(driver, 10))  .until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));

You have a plenty of methods in the ExpectedConditions class.

Here is some info:

Hope you find this useful.


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!


I also have a problem similar with you.

I have solved it.

Yes, I think we can insert delay or use a function, (...).findElements(...).size() , for a better performance. If the result of the function is not 0, then we can click or do else to the element.

According to "https://code.google.com/p/selenium/wiki/GettingStarted" and "WebDriver: check if an element exists?", we can insert delay and use the function to determine the existence of the element we wanted.

// Sleep until the div we want is visible or 5 seconds is over    long end = System.currentTimeMillis() + 5000;    while (System.currentTimeMillis() < end) {        List<WebElement> elements = driver.findElements(By.id("btn"));        // If results have been returned, the results are displayed in a drop down.        if (elements.size() != 0) {          driver.findElement(By.id("btn")).click();           break;        }    }

Wait until the element wanted is shown or time is up~!