In order to get the job done, you need to use org.openqa.selenium.interactions.Actions class. The Action class is user-facing API for emulating complex user gestures. WebDirver users can use this class to simulate usage of keyboard or mouse events. It Implements builder pattern. So calling build will build composite action containing all actions specified by the method calls.
I have used following code snippets to delete a tag in the tag cloud. The delete button of a particular tag gets visible only for mouse hover event.
Actions builder = new Actions(driver);
WebElement tagElement = driver.findElement(By.id("tag-cloud"));
builder.moveToElement(tagElement).build().perform();
You need to create new action builder instance by passing WebDriver instance to the class. Then move to the WebElement and build it to generates a composite action containing all actions so far, ready to be performed.
This will be the first post of Selenium WebDriver tips. Some useful aspects and solutions for common UI automation requirements using WebDriver will be published as i came across them.
23 comments:
Thanks for this Krishantha, I am new to selenium...please keep it coming, very informative and useful!
ranjana
Sir can you please tell us how to interact with mouseover elements in a template...it would be of great help if u would paste the code for interacting with mouseover elements..Thanks in advance
I started looking at selenium and your post proved to be a boon. Thanks a lot.
Hi Krish,
When i tried using the code you have mentioned i am getting the below error. Do we need to add any jar files?
org.openqa.selenium.InvalidElementStateException: Cannot perform native interaction: Could not load native events component.
Command duration or timeout: 71 milliseconds
Build info: version: '2.21.0', revision: '16552', time: '2012-04-11 19:09:00'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_05'
Driver info: driver.version: RemoteWebDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:175)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:128)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:459)
at org.openqa.selenium.remote.ExecuteMethod.execute(ExecuteMethod.java:47)
at org.openqa.selenium.remote.RemoteMouse.mouseMove(RemoteMouse.java:89)
at org.openqa.selenium.interactions.MoveMouseAction.perform(MoveMouseAction.java:37)
at org.openqa.selenium.interactions.CompositeAction.perform(CompositeAction.java:32)
at Admin_TC_001_01.TC_001(Admin_TC_001_01.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:702)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:894)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1219)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1192)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1117)
at org.testng.TestNG.run(TestNG.java:1025)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:109)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:202)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:173)
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Cannot perform native interaction: Could not load native events component.
Build info: version: '2.21.0', revision: '16552', time: '2012-04-11 19:09:00'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_05'
Driver info: driver.version: unknown
@Thomas - Are you using latest version of firefox? if yes, I recommend to use older firefox version 9 or 10 as WebDriver 2.20.0 or 2.21.0 is not yet fully compatible with latest version of firefox.
I understand that works completely clicking on the sub menu which is hidden but what if i have to enter values in the hidden object, can you please help me
Thanks a lot Krishantha, your code for Mouse mover events using Webdriver worked perfectly fine.
Thanks,
Krishnan
Hi, How do i hover over a tab and then click on one of the links displayed after hovering?
Like..
hover-> My Vodafone (tab)
Prepaid (1st link)
Postpaid (2nd link)(click)
Datacard (3rd link)
hi Krishantha's
Action builder code is working on WINDOWS machine but not on LINUX (UBUNTU) for Firefox
Any solution for the same..??
OMG, its fantastic!!! my search end up here for mouse over in webdriver.
Thank you very much Krshanth!!!!
Regards,
Narayanarao.Thota
Hi Krishantha, Can you tell me how to click on the drop down menus in C#? The code you provided is in Java. Thanks!
Hi Krishanta, Can you tell me how to click on a menu item in a drop down list in C#? I tried the example that you provided but its throwing error. Thanks!
@VG - i'm not much familiar with C#. You may find this post useful - http://stackoverflow.com/questions/5471515/webdriver-c-sharp-select-a-value-from-drop-down
Hi Krishan,
Can you please help me out on how to handle with Dynamic Xpath`s
for example: i need to enter FAcebook Status using script, but the Xpath is dynamic and i am not able to do so
Thanks
Suman
My problem is that fancy box not open until I move mouse pointer physically on selected image during test run or set the mouse pointer on screen where selected image will display before test run
Scenario:
I have gallery page in which different images paste on screen, when i click an image, fancy box open with next and previous buttons and user can move next and back to view all images in fancy box
WebElement el = driver.findElement(By.xpath("//div[2]/p/a/span"));
Actions builder = new Actions(driver);
builder.moveToElement(el).click();
builder.perform();
driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);
// following code also not work to move mouse pointer physically
Point coordinates = driver.findElement(By.xpath("//div[2]/p/a/span")).getLocation();
Robot robot = new Robot();
robot.mouseMove(coordinates.getX(),coordinates.getY()+120);
Please guide me if you have a solution, Thakns, Abdul Manan
abdulmanan.butt@gmail.com
hi krishan
i need to read the input values from excel for drop down box i have tried java executor too .it says
"'""""Element is not currently visible and so may not be interacted with
Command duration or timeout: 41 milliseconds"""""
Thank you very much, this solved my problem which stuck me for entire day !!
Please keep it up for the selenium tips
Hi Krishantha,
It is very nice blog for mouse hover event.
I have only class name for the button, so which code i can use for mouse hover on button with class name?
Can you please help me which code i can use for click on the button after mouse hover?
Thnaks,
Akash Gupta.
restart your system
thank you so munch brother
Post a Comment