Hello,
Welcome to Selenium tutorial, today we will see how to perform advance activity in Selenium Webdriver.
If you really want to automate critical applications, which include advance activity like Mouse Hover, Right click, Double click, Click and Hold, Keyboard activities and so on.
You cannot automate Ajax application, which contains advance activity so let us have a look.
you can also try Drag and Drop in Selenium using Action class
You do not have to worry about all this because all will come in single bundle i.e. you can perform all this using Actions class in Selenium.
Method name and Usage
moveToElement(WebElement)-- Mouse Hover contextClick()-- Right click on page contextClick(WebElement)-- Right click on specific Element sendKeys(KEYS.TAB)--For keyboard events clickAndHold(WebElement)--Click on element and hold until next operation release() Release the current control
We still have so many methods in Actions class so will recommend you have a look for other methods as well for better clarifications.
https://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/interactions/Actions.html
Usage of Mouse Hover- Handle Autosuggestion in Selenium
Now a days its default feature of almost all the application take an example of Google itself when you type some words on search box, it gives some related suggestion.
To achieve this we will use first mouse hover on element then click.
Scenario for Naukri.com autosuggestion-
First we will enter keywords using sendKeys() method then we have to wait for some time (2 or 3 seconds) to load suggestion and once it is loaded we will use mouse hover event using moveToElement() method of action class then we will click on that particular Item using click(Webelement) method of actions class.
Let’s implement the same
package testcases; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; public class AutoSuggestion { public static void main(String[] args) throws InterruptedException { WebDriver driver=new FirefoxDriver(); driver.manage().window().maximize(); driver.get("http://www.naukri.com"); // Type something on Skill textbox driver.findElement(By.id("qp")).sendKeys("test"); // Create object on Actions class Actions builder=new Actions(driver); // find the element which we want to Select from auto suggestion WebElement ele=driver.findElement(By.xpath(".//*[@id='autosuggest']/ul/li[2]/a")); // use Mouse hover action for that element builder.moveToElement(ele).build().perform(); // Give wait for 2 seconds Thread.sleep(2000); // finally click on that element builder.click(ele).build().perform(); } }
Right Click in Selenium Webdriver
As we discussed earlier for right-click on a particular link or any web-element Selenium Webdriver has contextClick() methods available in Actions class.
There are two flavors of this
1-contextClick()- which will right Click on a page
2-contextClick(WebElement) – which will right click on a particular web-element.
Program 1-Below is the program to right click on a link in Google Home page
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; import org.testng.annotations.Test; public class TestRightClick { @Test public void TestClick() throws Exception{ WebDriver driver=new FirefoxDriver(); driver.get(“http://www.google.com”); driver.manage().window().maximize(); Actions act=new Actions(driver); act.contextClick(driver.findElement(By.linkText(“Gujarati”))).perform(); } }
Keyboard events using Actions class.
For this we will use previous example after right click we will select second option from list for this we will use ARROW_DOWN key two times then we will hit ENTER Key.
import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; public class GoogleTC { public static void main(String[] args) { WebDriver driver=new FirefoxDriver(); driver.get("http://www.google.com"); driver.manage().window().maximize(); Actions act=new Actions(driver); act.contextClick(driver.findElement(By.linkText("Gujarati"))).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform(); } }
Hi @Mukesh Otwani, i am automating web application in which i am using event publishing time and starting, for this scenario i used publishing time as my system time but i want the starting time should be 6 mints later from the system time like suppose, publishing time in my system / current time is i.e. 1400 and want the starting time will be 1406, how can take 6 mints later plz help. thanks
Hi Faisal, You can capture current time and start new job (jenkins job) after 6 min. Are you using Jenkins?
Hi,I want to automate an api .I want to fetch some records for a particular date range from an api and print it . How to set the date dynamically? Please help me…
Hi Subhasmita,
Selection of date start date like 1 week before or 1 month before depends on your use case. Now for the end date, you can define how many days further for which you can Calendar class to get the end date dynamically based on your start date.
If you want the start date to be dynamic, you can fetch the system date dynamically and manipulate the start accordingly.
Hi Mukesh , the subscribe pop up of your website appears when i have already subscribed to it. Can it be controlled ?
Hi Yashu,
Its a website feature. If you have already subscribed, then simply cancel/close it
Hi Sir, Im trying to automate the signature screen my Frame is attached with Canvas there is no Id/xpath is defined for canvas, Canvas integration is directly done using java code is there any solution to write Xpath for canvas??
Hi Shabina,
Automation of canvas is not recommendable because locators never remain stable
Hi,
I am getting unsupported command exception how can I resolve this issue when I am trying to perform mouse hover using action class.
Chrome driver version- 77.0.3865.40
Chrome- 77.0.3865.90
Selenium web driver version-2.53.0
Hi Naincy,
Selenium version which you are using is very old, kindly use Selenium 3.141.59
Hi Mukesh,
Thanks for your valuable tutorial on Actions Class. Could I please know that the actions class work fine in Firefox browser..
Hi Sathish,
I made that post long back but it should work for latest Selenium version with compatible Firefox version following better matching gecko driver
Hi Mukesh,
My questions is apart from this page. I saw your tutorial about how to capture elements in frame http://learn-automation.com/handle-frames-in-selenium/
but on my website there is canvas tag and I am not able to find elements in canvas . Coule you please help me is this possible in selenium ?
Thanks
Hi Satveer,
Using selenium, it is difficult to locate elements under canvas tag because it keep on changing dynamically
I have written the below code by taking reference from your blog
package com.Mypackage;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
public class MouseHover {
public static void main(String []args) throws InterruptedException{
System.setProperty(“webdriver.gecko.driver”,”C:\\javapra\\geckodriver-v0.13.0-win64\\geckodriver.exe”);
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get(“https://www.naukri.com/”);
WebElement element=driver.findElement(By.xpath(“//span[text()=’ Search Jobs ‘]”));
element.click();
element.sendKeys(“test”);
Actions act=new Actions(driver);
WebElement ele=driver.findElement(By.xpath(“.//*[@id=’sugDrp_skill’]/ul/li[1]/div”));
act.moveToElement(ele).build()perform();
act.click(ele).build().perform();
}
}
Getting this exception:
Exception in thread “main” org.openqa.selenium.UnsupportedCommandException: POST /session/54395cd1-96ff-4ef9-859f-3e8ee68cdb73/moveto did not match a known command.
I have googled it and that selenium version 3 and above are not supporting action commands.Is it true, please reply.
Hi Shobhit,
Its better if you downgrade your FF and selenium version to 2.53.1.
Action command has problems only with firefox . Try with chrome It should work
Hi Shobhit,
Gaurav is right, give a try to chrome.
Mukesh, I am getting nosuchelementexception for the below code; not sure why; can you help me out here
driver.get(“http://www.google.co.in”);
driver.findElement(By.xpath(“.//*[@id=’gs_htif0′]”)).sendKeys(“kavitha”);
Actions act = new Actions(driver);
WebElement ele = driver.findElement(By.xpath(“.//*[@id=’sbse0′]/div[2]”));
act.moveToElement(ele).build().perform();
Thread.sleep(2000);
act.click(ele).build().perform();
Hi Kavitha,
I can’t see these xpaths which you have taken for locator on http://www.google.co.in . Please recheck these xpath values once again.
Hello Mukesh,
I came across this error- unsupported command exception.POST /session/dcf6738a-b46f-a342-9f46-c5d5f8e041b9/moveto did not match a known command. I am using selenium 3 and FF browser version 45.7.0.Can you pease help me with this.
Hi Ramya,
FF 45 is quite old version which I think doesn’t provide support for Selenium 3. so please upgrade your FF.
How to right click on link and then open it in new window
Hi Sangeeta,
Below post will guide you for the same.
http://learn-automation.com/selenium-advance-activity/
By.xpath(“.//*[@id=’autosuggest’]/ul/li[2]/a”));
i do’t get also autosuggst
plz define .
Link please?
how to handle multiple windows and how to use mouse hover using page object model with page factory. i tried many methods but am unable to do it. can u please clarify with this?
Hi Bindu,
This will help https://plus.google.com/+Mukeshotwani/posts/c4EL2owC7xQ
Right click example for Google doesnt work. It throws error. This is because the google page is changed.
Yeah try with some stable application
When I open naukri, my script fails saying Element not found exception.
Hey Nikhil,
Kindly provide more info
Hi Mukesh , I am not able to find the xpath of the autosuggestion , its not working for me .
For which application you are trying..
why did you used sendKeys(Keys.ARROW_DOWN) multiple times in keyword event block???
Hi Sathya,
If you want to select third option then you have to press three time arrow down.
Hi Mukesh,
what is the difference between perform() and build().perform() ?
I tried the naukri.com example with just “perform()” method only and it was giving me the same results.
Thanks,
Amit Chaudhary
Hi Amit,
If you have single operation then its ok but when you have multiple operation then build().perform() is must.
Hi Mukesh , thanks for such a nice blog….
can you give an example of build and perform …..
Hi Amit,
Whenever you use Actions class, the build and perform come into picture. Suppose if you are sending multiple keyboard events.