Selenium-Webdriver (Java) cannot execute "hover and click" function sequentially

I am trying to access the screen in an application that appears when I hover over a tab and click on one of the options. I used the Actions method to accomplish this using selenium. Here's my code:

element=driver.findElement(By.id("tab"));
Actions hoverover=new Actions(driver);
hoverover.moveToElement(element).moveToElement(driver.findElement(By.id("menu"))).click().build().perform();

      

When I enter the application and call this tab directly, I can access it without any problem. But the problem occurs when I access this tab from another screen in the application.

Whenever I access the hover page from another page in the application, sometimes the page loads correctly, but most of the time it fails and I get a "no such item" or "outdated item link" error.

I'm really not sure how he can access the tab without any problem and sometimes throws errors. Please direct me here and let me know if there is anything else (any additional features or alternatives to actions?) I can make the mouse click work all the time.

EDIT: I've tried using both Explicit and Implicit Waits and even thread.sleep, but to no avail. In Chrome (chrome only), when I do a manual screen refresh when it tries to access the tab, it works. But when I do the same in my code [driver.navigate (). Refresh ()], it doesn't work !!

+3


source to share


1 answer


Element Exception probably happens between when you set the element and when you hoverover. Selenium does something like:

WebElement element = driver.findElement(By.id("tab"));
// "element" has been set
Actions hoverover=new Actions(driver);
// Between here and hoverover, "element" has changed on the DOM
hoverover.moveToElement(element).moveToElement(
    driver.findElement(By.id("menu"))).click().build().perform();
// Uh-oh, what "element?" Better throw an exception!

      

Try to exclude the element = line and move the driver.findElement inside moveToElement ().



Actions hoverover = new Actions(driver);
hoverover.moveToElement(driver.findElement(By.id("tab")))
    .moveToElement(driver.findElement(By.id("menu"))).click().build().perform();

      

You can also try throwing in WebDriverWait between hovering over the tab and menu.

hoverover.moveToElement(driver.findElement(By.id("tab"))).build().perform();
new WebDriverWait(driver, 10))
    .until(ExpectedConditions.presenceOfElementLocated(By.id("menu")));
hoverover.moveToElement(driver.findElement(By.id("menu"))).click().build().perform();

      

0


source







All Articles