How to select sub menu by mouse hovering over the main menu?

I knew few of the concepts but still exploring new things like I am not able to detect how to write code for the selection of menu items from the main menu. I am unable to find the menu item element using  XPATH. Like for sample for the site www.toolsqa.com if I hover the mouse on “TUTORIALS” main menu the sub menu is shown.

 

 

 

 

I have written the code like as below and it is running successfully and detected the main menu “TUTORIALS”  but not able to detect the sub menu “Selenium”. I am finding difficulty to locate the menu item that needs to mouse over on could you please suggest me where the mistake is?

public static void main(String[] args) throws InterruptedException {

WebDriver driver= new FirefoxDriver();
driver.manage().window().maximize();
driver.get(“http://www.toolsqa.com/”);
Thread.sleep(5000);
//finding the main menu item “TUTORIALS” using XPATH
WebElement menu= driver.findElement(By.xpath(“.//*[@id=’main-nav’]/li[2]/a”));
Thread.sleep(5000);

//Move mouse pointer on drop down menu item “Selenium”
Actions actions= new Actions(driver);
actions.moveToElement(menu).perform();
driver.findElement(By.linkText(“Selenium”)).click();

}
}

 

 

When I tried to locate the menu item using FireBug it displays as below. Can’t we use “linktext” to locate the sub menu item?

 

 

 

Finally I am very sorry to bother you with my questions. Is it alright if I come with new questions ?

 

Thanks and regards,

Kavitha

Train Asked on September 5, 2015 in Selenium WebDriver.
Add Comment
  • 4 Answer(s)

    This makes you move to main menu

     actions.moveToElement(mainmenu).build().perform();
     

    This makes you click the sub menu from the main menu

     actions.moveToElement(mainmenu).moveToElement(submneu).click().build().perform();
     

    Please try this and let us know if you are still facing the issue

    Supporter Answered on September 6, 2015.
    Add Comment

    Hello Bheem Reddy

    Thanks for providing the solution.  But could you please let me know how can I initialise the sub menu ? It gives me error message when I select the name of the sub menu item “Selenium”. How can I detect the sub menu item using XPATH?

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    WebDriver driver= new FirefoxDriver();
    driver.manage().window().maximize();
    driver.get(“http://www.toolsqa.com/”);
    Actions actions= new Actions(driver);
    WebElement menu= driver.findElement(By.xpath(“.//*[@id=’main-nav’]/li[2]/a”));
    actions.moveToElement(menu).build().perform();
    actions.moveToElement(menu).moveToElement(“Selenium”).click().build().perform();

    Train Answered on September 6, 2015.
    Add Comment
    try
    
    {
    WebDriver driver= new FirefoxDriver();
    
    driver.manage().window().maximize();
    
    driver.get("http://www.toolsqa.com/");
    
    Actions actions= new Actions(driver);
    
    WebElement menu= driver.findElement(By.xpath(".//*[@id='main-nav']/li[2]/a"));
    
    actions.moveToElement(menu).build().perform();
    
    actions.moveToElement(driver.findElement(By.xpath(".//*[@id='main-nav']/li[2]/ul/li[2]/a/span"))).click().build().perform();
    
    }
    
    catch(Exception e)
    
    {
    
    e.printStackTrace();
    
    }
    
    Supporter Answered on September 6, 2015.
    Add Comment

    Thank you very much really it worked.

    Train Answered on September 6, 2015.

    Welcome .

    For further Queries please share the Screenshot so that it adds more info 🙂

    on September 6, 2015.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.