Friday, February 7, 2014

Hover the Mouse on a menu and click on a dropdown menu option

public class HoverAndClickOnAMenu {
public static void main(String[] args) {

//initialise browser
WebDriver driver = new FirefoxDriver();
driver.get("http://www.ticketmaster.co.uk");

//locate the menu to hover over using its xpath
WebElement menu = driver.findElement(By.xpath("//*[@id='music']"));

//Initiate mouse action using Actions class
Actions builder = new Actions(driver); 

// move the mouse to the earlier identified menu option
builder.moveToElement(menu).build().perform();

// wait for max of 5 seconds before proceeding. This allows sub menu appears properly before trying to click on it
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='subNav_music_menu']/tbody/tr[2]/td[1]/a[1]")));  // until this submenu is found

//identify menu option from the resulting menu display and click
WebElement menuOption = driver.findElement(By.xpath("//*[@id='subNav_music_menu']/tbody/tr[2]/td[1]/a[1]"));
menuOption.click();
}
}

public class HoverAndClickOnAMenu {
public static void main(String[] args) {

//initialise browser
WebDriver driver = new FirefoxDriver();
driver.get("http://www.ticketmaster.co.uk"); 

//locate the menu to hover over using its xpath
WebElement menu = driver.findElement(By.xpath("//*[@id='music']"));

//Initiate mouse action using Actions class
Actions builder = new Actions(driver);   

// move the mouse to the earlier identified menu option
builder.moveToElement(menu).build().perform();

// wait for max of 5 seconds before proceeding. This allows sub menu appears properly before trying to click on it
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='subNav_music_menu']/tbody/tr[2]/td[1]/a[1]")));  // until this submenu is found

//identify menu option from the resulting menu display and click
WebElement menuOption = driver.findElement(By.xpath("//*[@id='subNav_music_menu']/tbody/tr[2]/td[1]/a[1]"));
menuOption.click();
}
} - See more at: http://www.logicandtricks.com/articles/hover-mouse-menu-and-click-resulting-dropdown-menu-option#sthash.XNT8wK5B.dpuf
public class HoverAndClickOnAMenu {
public static void main(String[] args) {

//initialise browser
WebDriver driver = new FirefoxDriver();
driver.get("http://www.ticketmaster.co.uk"); 

//locate the menu to hover over using its xpath
WebElement menu = driver.findElement(By.xpath("//*[@id='music']"));

//Initiate mouse action using Actions class
Actions builder = new Actions(driver);   

// move the mouse to the earlier identified menu option
builder.moveToElement(menu).build().perform();

// wait for max of 5 seconds before proceeding. This allows sub menu appears properly before trying to click on it
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id='subNav_music_menu']/tbody/tr[2]/td[1]/a[1]")));  // until this submenu is found

//identify menu option from the resulting menu display and click
WebElement menuOption = driver.findElement(By.xpath("//*[@id='subNav_music_menu']/tbody/tr[2]/td[1]/a[1]"));
menuOption.click();
}
} - See more at: http://www.logicandtricks.com/articles/hover-mouse-menu-and-click-resulting-dropdown-menu-option#sthash.XNT8wK5B.dpuf