Tuesday 8 December 2015

Print DropDown Menu in Selenium + MouseOver in Selenium

How should we print DropDown Menu list which appear on MouseOver?




Sample Code:


// Open Browser
WebDriver driver = new FirefoxDriver();
// Open WebSite
driver.get("http://www.amazon.in/");
// WebElement os 'Shop By Category'
WebElement shopBy = driver.findElement(By.xpath(".//*[@id='nav-link-shopall']/span[2]"));// Actions as we have to do mouse event .. Need to move mouse over to 'Shop By Category'.
Actions actions = new Actions(driver);
actions.moveToElement(shopBy).build().perform();
// Runtime, we get all Navigation element, so if any new element added then also we can get that element in print list.
List<WebElement> navigations = driver.findElements(By.xpath("//*[@id='nav-flyout-shopAll']/div[@class='nav-template nav-flyout-content nav-tpl-itemList']/span[@role='navigation']/span[@class='nav-text']"));
for(WebElement navigation : navigations)
System.out.println(navigation.getText());
// Close Browser

driver.close();