Saturday, July 11, 2015

Action class, mouse over actions

Actions <object name>= new Actions("<driver object>");

<object name>.moveToElement(<driver object>.findElement(By.linkText("<value>")).build().perform();  




 ----- Action part will be working within the browser.

 Here for mouse over we need to create Actions object by giving drivers object. And using Actions object we can do moveToElement method on a web element.


- to perform few more operations after moving the mouse

After you make moveToElement is made, if you have list of options in it, so better do again moveToElement and then find the element and click it.

- Source: http://stackoverflow.com/questions/17293914/how-to-perform-mouseover-function-in-selenium-webdriver-using-java

Code:
  1. Hover over a menu item.
  2. Find the hidden element that is ONLY available after the hover.
  3. Click the sub-menu item.
If you insert a 'perform' command after the moveToElement, it moves to the element, and the sub-menu item shows for a brief period, but that is not a hover. The hidden element immediately disappears before it can be found resulting in a ElementNotFoundException. I tried two things:
Actions builder = new Actions(driver);
builder.moveToElement(hoverElement).perform();
builder.moveToElement(clickElement).click().perform();
This did not work for me. The following worked for me:
Actions builder = new Actions(driver);
builder.moveToElement(hoverElement).perform();
By locator = By.id("clickElementID");
driver.click(locator);

Using the Actions to hover and the standard WebDriver click, I could hover and then click.



Scenario:

To click the sign in button in Amazon home page:
Solution:


WebElement obj2 = obj1.findElement(By.xpath("//*[@id='nav-link-yourAccount']/span[2]")); //My account element
 

Actions act1  = new Actions(obj1);


act1.moveToElement(obj2).perform();

act1.moveToElement(obj2).moveToElement(obj2.findElement(By.xpath("//*[@id='nav-flyout-ya-signin']/a/span"))).click().build().perform();

support: http://stackoverflow.com/questions/17293914/how-to-perform-mouseover-function-in-selenium-webdriver-using-java

Notes:
moveToElment() - Moves the mouse to an offset from the top-left corner of the element.
perform() - A convenience method for performing the actions without calling build() first.
build() - Generates a composite action containinig all actions so far, ready to be performed (and resets the internal builder state, so subsequent calls to build() will contain fresh sequences).


No comments:

Post a Comment