Saturday, April 18, 2015

Handling menu & Sub-Menu’s in Selenium

Handling menu & Sub-Menu’s in Selenium Using Actions Class

In this Topic I am going to explain how we can navigate to Menus’ and SubMenus and perform actions using the Action Class.

Let us suppose, we want to click T-Shirts under the Mens Section on Flipkart.com, for that I have to do below steps:

  • Open Flipkart.com
  • Move the mouse to Men’s on Toolbar (Make note that we don’t click here, the submenu opens on hover automatically)
  • Click on T-Shirt


For above , you will need xpath of Mens (Main Menu) & t-Shirt (Sub-menu)

Below is the sample code using Actions Class.

public static void main (String args[]) throws InterruptedException{
             
              WebDriver driver = new FirefoxDriver();
              driver.get("http://flipkart.com");
             
              //Save the Xpaths in a String
             
              String Menu = "//*[@id='fk-mainhead-id']/div[2]/div/div[1]/ul/li[3]";
              String SubMenu = "//*[@id='menu-men-tab-0-content']/ul[2]/li[3]/a";
             
              WebElement menu = driver.findElement(By.xpath(Menu));
              WebElement submenu = driver.findElement(By.xpath(SubMenu));
             
              Actions action = new Actions(driver);
              action.moveToElement(menu).perform();
              Thread.sleep(2000L);
              submenu.click();
             
             

       }

No comments:

Post a Comment