There will be a situation where you need to hover on a menu item to see the sub items listed under it. Selenium might not recognize the link text to be selected within the short fraction of time the sub menu item visible. In this case we can use Action class to move the focus to the sub menu item to help selenium in recognizing the web element.
Below code snippet can be used to achieve this
WebElement element = driver.findElement(By.linkText("Menu"));
Actions action = new Actions(driver);
action.moveToElement(element).perform();
WebElement subElement = driver.findElement(By.linkText("SubMenu"));
action.moveToElement(subElement).build().perform();
Below code snippet can be used to achieve this
WebElement element = driver.findElement(By.linkText("Menu"));
Actions action = new Actions(driver);
action.moveToElement(element).perform();
WebElement subElement = driver.findElement(By.linkText("SubMenu"));
action.moveToElement(subElement).build().perform();

