- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to loop through a menu list on a webpage using Selenium?
We can loop through a menu list on a webpage using Selenium webdriver.
In a webpage, a list is represented by an ul tag and it consists of elements with li tag. Thus the li tag can be said as the child of ul.
First, we have to identify the element with ul tag with any locator, then traverse through its li sub-elements with the help of a loop. Finally, use the method getText to obtain the text on the li elements.
Let us try to identify the menu list on a webpage.
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import java.util.concurrent.TimeUnit; import java.util.List; public class MenuItemLst{ public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "C:\Users\ghs6kor\Desktop\Java\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); //implicit wait driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); //URL launch driver.get("https://www.tutorialspoint.com/about/about_careers.htm"); // identify elements in menu with findElements List<WebElement> p = driver. findElements(By.xpath("//ul[@class='toc reading']/li")); System.out.println("Menu Items are: "); //iterate through list for( WebElement i: p){ System.out.println(i.getText()); driver.quit();} } } }
Output
- Related Articles
- How to send keyboard input to a textbox on a webpage using Python Selenium webdriver?
- How to refresh a webpage using Python Selenium Webdriver?
- How can I verify Error Message on a webpage using Selenium Webdriver?
- How to scroll down a webpage in selenium using Java?
- How to find Elements in a Webpage using JavaScript in Selenium?
- How to add a custom right-click menu to a webpage in JavaScript?
- Is it possible to scroll down in a webpage using Selenium Webdriver programmed on Python?
- Loop through a Set using Javascript
- How to iterate a Java List using For Loop?
- Loop through a hash table using Javascript
- How to send a report through email using Selenium Webdriver?
- How to loop through multiple lists using Python?
- How to get screenshot of full webpage using Selenium and Java?
- How to iterate a Java List using For-Each Loop?
- How to iterate a List using for Loop in Java?

Advertisements