Skip to main content

You are here

List all the options in a menu

The following loads a website then prints off the text on the links in the main menu. This is a very simple code. Its actually not of very much use except it can be adapted to Assert if certain menu links exist. I am re-writting it to check for the presence of designated menu options. For example to check if the menu contains "Home" or "Forums". Watch out for that next.

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;

public class AssertMenuOptionsText {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver(); //or some other driver
WebElement mainMenu = driver.findElement("main-menu"));
List<WebElement> menuOptions = mainMenu.findElements(By.tagName("a"));

System.out.println(menuOptions.size() + " menu options found!");
for (int i=0;i<menuOptions.size();i++){




No votes yet

Add new comment

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.


Note that the codes and ideas expressed on this site are the authors personal views. The content of this website are the personal notes based on the learning experience of the author. The author will not be responsible for any error or damages resulting from you using the content of this website.

Premium Drupal Themes by Adaptivethemes