I'm trying to write Webdriver tests where I need to hover the mouse cursor over an element to trigger a drop down menu, and then click a button from the drop down menu. I've been writing my code following the suggestion from How to perform mouseover function in Selenium WebDriver using Java?. So for example, my code might look like this :
Actions action = new Actions(webdriver);
WebElement hoverElem = webdriver.findElement(By.xpath("//html/body/div[13]/ul/li[4]/a"));
WebElement clickElem = webdriver.findElement(By.xpath("//html/body/div[3]/li[12]/a"));
action.moveToElement(hoverElem).moveToElement(clickElem).click().build().perform();
My code runs perfectly when I test it in Firefox, but in Chrome it's inconsistent; sometimes it will work perfectly, and then the next time I run the test it will fail. In Opera it never works. When the code fails, it looks like the drop down menu appears for a split second on the screen, then disappears before WebDriver can click on the button on the drop down menu. I'm not sure how I can fix this problem. How can I get this to work with all 3 browsers?
As a reference, I'm using selenium-2.53.0, Chrome 53.0.2785.101 64-bit, and Opera 39.0.2256.71 64-bit.