Monday, 23 September 2013

Capturing the Sub Menu values using Selenium Web Driver

This program opens url and prints all submenu of Home menu.

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class ReadSubMenu {

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver(); //object created to start firefox driver

Actions action = new Actions(driver); // object created to perfomr mouse operation

driver.get("http://demo.lateralcode.com/css-drop-down-menus/"); // open url

WebElement home = driver.findElement(By.linkText("Home")); // find home menu

action.moveToElement(home).build().perform(); //put mouse pointer on Home menu

List we = home.findElements(By.xpath("//*[@id='menu']/ul/li[1]/ul/li")); // common property of all sub menu item

//travers through all submenu & print their text
for (int i =0; i
{
System.out.println(we.get(i).getText()); //print submenu text
}
 }

}

No comments:

Post a Comment