- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Select item from sub-menu of a menu using mouse over action in Selenium
We can select an item from the sub-menu of a menu using mouse over action in Selenium webdriver with the help of the Actions class. We shall create an object of the Actions class and then apply moveToElement to it.
This method shall move the mouse to the middle of the menu which displays submenu on mouse over. Then apply the perform method to actually perform this action. After hovering on the menu, we shall select a sub-menu with the help of the click method.
Syntax
WebElement n=driver.findElement(By.id("nav-link-accountList")); Actions a = new Actions(driver); a.moveToElement(n).perform();
Let us hover on the below highlighted menu on the page and then select a submenu – Create a List −
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 org.openqa.selenium.interactions.Action; import org.openqa.selenium.interactions.Actions; public class SubMenuClick{ 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.amazon.com/"); //identify menu WebElement n=driver.findElement(By.id("nav-link-accountList")); // object of Actions with method moveToElement Actions a = new Actions(driver); a.moveToElement(n).perform(); //identify sub-menu element WebElement m=driver. findElement(By.xpath("//*[text()='Create a List']")); //move to element and click a.moveToElement(m).click().perform(); System.out.println("Page navigated to: " +driver.getTitle()); driver.quit(); } }
Output
- Related Articles
- JavaFX example to set action (behavior) to the menu item
- Create a command/menu item that the user can invoke from a popup menu in HTML5
- How to disable a menu item in JavaFX
- How to select a drop-down menu option value with Selenium (Python)?
- How to add accelerators to a menu item?
- How to add a separator in Menu item in Tkinter?
- How to change the Text color of Menu item in Android using Kotlin?
- How to loop through a menu list on a webpage using Selenium?
- How to add the slider to a menu item in JavaFX?
- How to change the text color of Menu item in Android?
- How can we create a JPopupMenu with a sub menu in Java?
- How to add image to the menu item in JavaFX?
- Creating a Dropdown Menu using Tkinter
- How to select an item from a dropdown list using Selenium WebDriver with java?
- How do I hide and show a menu item in the Android ActionBar?

Advertisements