How to select the submenu of the flyout using selenium webdriver - selenium

I want to select the submenu of the flyout using selenium webdriver.
Also how can I simulate on hover on menu items using selenium.
Please guide or provide some tutorial for the same.
Thanks

Check the actions class, something like this you need:
WebElement element = driver.findElement(By.Id("id"));
Actions builder = new Actions(driver);
Actions hover = builder.moveToElement(element);
hover.build().perform();

String New_select_Just_Landed = "//*[#id='nav1']/a";
WebElement Ba = driver.findElement(By.xpath(New_select_Just_Landed));
WebElement subBa = driver.findElement(By.xpath("//*[#id='nav1']/section/ul/li/a"));
Actions action = new Actions(driver);
action.moveToElement(Ba).perform();
Thread.sleep(2000); System.out.println("Mouse hoover succeed");
action.moveToElement(subBa).perform();
subBa.click();
System.out.println("Click Just Landed! succeed");
Thread.sleep(5000L);

Related

Selenium java webdriver 3: moveToElement not working

Selenium java webdriver 3: moveToElement not working.
WebElement element = ...
Actions actions = new Actions(driver);
actions.moveToElement(element).build().perform();
Tried, adding click()
WebElement element = ...
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();
Not working. The mouse is not moved.
WebElement abcd = ..........
Actions actions = new Actions(driver);
actions.moveToElement(abcd).perform();
Above code will work, but please check with the chrome.exe version and the chrome version you are using it your machine. both should be compatible to one another. Check the compatibility with below link.
https://sites.google.com/a/chromium.org/chromedriver/
Skip the build() part, perform() does it underneath anyway.
Actions actions = new Actions(driver);
WebElement element = driver.findElement(By.linkText("host"));
actions.moveToElement(element).build().perform();
This will work. first check your "find element" method is write or wrong. Please post this step as well. otherwise your code is correct.
try to find element by xpath rather than link text. it worked for me.
WebElement element = driver.findElement(By.xpath("..."));
Actions actions = new Actions(driver);
actions.moveToElement(element).build().perform();
Try below code
public static void mouse_movement(WebDriver driver, String xpathExpression) {
Actions act = new Actions(driver);
WebElement target = driver.findElement(By.xpath(xpathExpression));
act.moveToElement(target).build().perform();
}
if you need to click the element you could try javascript:
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", driver.findElement(By.xpath(xPath)));
I have solved this problem with:
const element = await driver.findElement(...)
await driver.executeScript("arguments[0].scrollIntoView(true);", element)
await driver.sleep(500);

NoSuchElementException on performing Mousehover action in selenium

I have a webpage in which I am trying to perform a mouse hover.. There is an element named "Entity Records" on which if you hover your mouse it displays a menu. In that menu I need to click on an element named Create New record.
I have tried 3 different set of codes but it's not working for me..
1st One:
WebElement el = driver.findElement(By.xpath("\\...."));
action.moveToElement(el).build().perform();
Actions builder = new Actions(driver);
builder.moveToElement(driver.findElement(By.cssSelector("..."))).click().build().perform();
2nd one:
WebElement entityrecordsmenu = driver.findElement(By.cssSelector("..."));
Actions builder = new Actions(driver);
builder.moveToElement(entityrecordsmenu).build().perform();
WebElement createnewrecord = driver.findElement(By.cssSelector("..."));
createnewrecord.click();
3rd one:
Actions action2 = new Actions(driver);
WebElement ele = driver.findElement(By.xpath("...));
action2.moveToElement(ele).build().perform();
Actions build2 = new Actions(driver);
build2.moveToElement(driver.findElement(By.xpath("..."))).click().build().perform();
1st and 2nd code are doing the mouse hover fine, but its not clicking on the menu element. Can anyone suggest me the correct way of doing this?
After mouse hover, try by giving some wait to get that element displayed and then click.
WebElement entityrecordsmenu = driver.findElement(By.cssSelector("..."));
Actions builder = new Actions(driver);
builder.moveToElement(entityrecordsmenu).build().perform();
//provide wait here to display dropdown
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//some time implicity wait may fails then use thread.sleep
//Thread.sleep(3000);
WebElement createnewrecord = driver.findElement(By.cssSelector("..."));
createnewrecord.click();
//if still above once does not work you can try
Actions builder1 = new Actions(driver);
builder1.moveToElement(entityrecordsmenu).click(driver.findElement(By.cssSelector("..."))).build().perform();
Thank You,
Murali

Unable to click a value from a sub list of the main list using mouse over action of selenium webdriver

I am trying to automate a scenario where I can select a value from a sub-menu present under main menu.
Below is the application url:
http://www.jetairways.com/EN/IN/Home.aspx
Its a mouser over functionality and tried to automate the Tab (Plan your travel-> Flights -> Book Online) with the below code but not working:
WebElement we = driver.findElement(By.xpath(".//*[#id='PlanYourTravel']/span/b"));
WebElement we1 = driver.findElement(By.xpath(".//*[#id='lnkThirdLevel58']"));
WebElement we2 = driver.findElement(By.xpath(".//*[#id='ddsubSubmenu58']/li[1]/a"));
Actions builder = new Actions(driver);
WebDriverWait wait = new WebDriverWait(driver, 15, 100);
builder.moveToElement(we).perform();
Thread.sleep(10000);
wait.until(ExpectedConditions.visibilityOf(we1));
builder.moveToElement(we1).perform();
wait.until(ExpectedConditions.visibilityOf(we2));
builder.moveToElement(we2).click().perform();
I am getting the element not visible issue. Quick help on this would be much appreciated.
I have tried with below code to click on "Book Online" and its working fine
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
driver.get("http://www.jetairways.com/EN/IN/Home.aspx");
Actions actObj = new Actions(driver);
//actObj.moveToElement(driver.findElement(By.xpath("//*[text()='Plan Your Travel']"))
actObj.moveToElement(driver.findElement(By.xpath("//*[#id='PlanYourTravel']/span/b"))).perform();
actObj.moveToElement(driver.findElement(By.xpath("//li[#title='Flights']/a"))).perform();
driver.findElement(By.xpath("//a[#title='Book Online']")).click();

How to do mouseover using webdriver actions?

I'm trying to get element that appear after a mouseover action. how to do ?
I tried:
Actions action = new Actions(driver);
action.moveToElement(elem);
action.perform();
WebElement myDynamicElement = (new WebDriverWait(driver,10)).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("class*='hoverEverywhereTooltip'")));
this element appear just after the mouse over event.
Try this.
code:
Actions actions = new Actions(driver);
WebElement menuhover = driver.findElement(By.linkText("Menu"));
actions.moveToElement(menuhover);
WebElement subLink = driver.findElement(By.id("submenu"));
actions.moveToElement(submenu);
actions.click();
actions.perform();
it worked I missed [] in my cssSelector properties:
WebElement myDynamicElement = (new WebDriverWait(driver,10)).until(ExpectedConditions.presenceOfElementLocated(By.c‌​ssSelector("[class*='hoverEverywhereTooltip']")));

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?
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)
Simulate mouseOver event and then select element you can like that:
var elementToShowMenu = Driver.FindElement(Byl.Id("some id"));
new Actions(Driver).MoveToElement(elementToShowMenu).Release(elementToShowMenu).Build().Perform();
var menuElement = Driver.FindElement(Byl.Id("your menu id"));
Here is how I click a invisible anchor link on a tag: a link that is generated dynamically by Javascript:
public static void mouseClickByLocator( String cssLocator ) {
String locator = cssLocator;
WebElement el = driver.findElement( By.cssSelector( locator ) );
Actions builder = new Actions(driver);
builder.moveToElement( el ).click( el );
builder.perform();
}
WebElement mnuElement;
WebElement submnuElement;
mnuElement = driver.findElement(By.cssSelector("insert selector here"));
submnuElement = driver.findElement(By.cssSelector("insert selector here"));
Actions builder = new Actions(driver);
// Move cursor to the Main Menu Element
builder.moveToElement(mnuElement).perform();
// Pause 2 Seconds for submenu to be displayed
TimeUnit.SECONDS.sleep(2); //Pause 2 seconds
// Clicking on the Hidden submnuElement
submnuElement.click();