Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select an element from a menu using Webdriver Selenium ? The Menu drop down shows up on Mouse Over?

How to select an element from a menu using Webdriver Selenium ? The Menu drop down shows up on Mouse Over?

like image 894
user1511808 Avatar asked Sep 05 '12 06:09

user1511808


1 Answers

You can check it in two ways: 1) first way is to use actions builder

WebElement mnuElement;
WebElement submnuElement;
mnEle = driver.findElement(By.Id("mnEle")).Click();
sbEle = driver.findElement(By.Id("sbEle")).Click();

Actions builder = new Actions(driver);
// Move cursor to the Main Menu Element
builder.MoveToElement(mnEle).Perform();
// Giving 5 Secs for submenu to be displayed
Thread.sleep(5000L);
// Clicking on the Hidden SubMenu
driver.findElement(By.Id("sbEle")).Click();

See here

2) another approach is to click directly needed element using jscript without simulating mouse hover event:

String cssLocatorOfTheElement=....//locator of the element to click on    
JavascriptExecutor js = (JavascriptExecutor) driver;
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("var x = $(\'"+cssLocatorOfTheElement+"\');");
            stringBuilder.append("x.click();");
            js.executeScript(stringBuilder.toString());

hope this works for you)

like image 153
eugene.polschikov Avatar answered Oct 06 '22 01:10

eugene.polschikov