• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Basic Selenium / Selenium Advance Activity

Selenium Advance Activity

April 3, 2015 by Mukesh Otwani 40 Comments

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();

}

}

 

Filed Under: Basic Selenium

Reader Interactions

Comments

  1. Faisal nadeem says

    March 1, 2022 at 2:35 AM

    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

    Reply
    • Mukesh Otwani says

      March 9, 2022 at 9:48 AM

      Hi Faisal, You can capture current time and start new job (jenkins job) after 6 min. Are you using Jenkins?

      Reply
  2. Subhasmita says

    February 10, 2022 at 11:48 PM

    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…

    Reply
    • Mukesh Otwani says

      February 13, 2022 at 11:16 PM

      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.

      Reply
  3. Yashu says

    April 2, 2020 at 11:18 PM

    Hi Mukesh , the subscribe pop up of your website appears when i have already subscribed to it. Can it be controlled ?

    Reply
    • Mukesh Otwani says

      April 3, 2020 at 6:21 PM

      Hi Yashu,

      Its a website feature. If you have already subscribed, then simply cancel/close it

      Reply
  4. shabina shaikh says

    October 14, 2019 at 12:44 PM

    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??

    Reply
    • Mukesh Otwani says

      October 14, 2019 at 4:48 PM

      Hi Shabina,

      Automation of canvas is not recommendable because locators never remain stable

      Reply
  5. Naincy says

    September 25, 2019 at 6:14 PM

    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

    Reply
    • Mukesh Otwani says

      September 25, 2019 at 6:32 PM

      Hi Naincy,

      Selenium version which you are using is very old, kindly use Selenium 3.141.59

      Reply
  6. Sathish Kumar says

    August 27, 2019 at 1:27 PM

    Hi Mukesh,

    Thanks for your valuable tutorial on Actions Class. Could I please know that the actions class work fine in Firefox browser..

    Reply
    • Mukesh Otwani says

      August 27, 2019 at 3:35 PM

      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

      Reply
  7. satveer bajwa says

    June 28, 2019 at 8:04 PM

    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

    Reply
    • Mukesh Otwani says

      June 29, 2019 at 3:00 PM

      Hi Satveer,

      Using selenium, it is difficult to locate elements under canvas tag because it keep on changing dynamically

      Reply
  8. Shobhit says

    February 18, 2017 at 2:20 PM

    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.

    Reply
    • Mukesh Otwani says

      February 18, 2017 at 7:00 PM

      Hi Shobhit,

      Its better if you downgrade your FF and selenium version to 2.53.1.

      Reply
    • Gaurav Khurana says

      February 19, 2017 at 4:27 PM

      Action command has problems only with firefox . Try with chrome It should work

      Reply
      • Mukesh Otwani says

        February 20, 2017 at 6:44 AM

        Hi Shobhit,

        Gaurav is right, give a try to chrome.

        Reply
  9. Kavitha says

    February 10, 2017 at 12:33 PM

    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();

    Reply
    • Mukesh Otwani says

      February 10, 2017 at 6:55 PM

      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.

      Reply
  10. Ramya says

    February 7, 2017 at 1:38 PM

    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.

    Reply
    • Mukesh Otwani says

      February 10, 2017 at 7:19 PM

      Hi Ramya,

      FF 45 is quite old version which I think doesn’t provide support for Selenium 3. so please upgrade your FF.

      Reply
  11. sangeeta says

    October 30, 2016 at 5:40 PM

    How to right click on link and then open it in new window

    Reply
    • Mukesh Otwani says

      October 31, 2016 at 3:36 AM

      Hi Sangeeta,

      Below post will guide you for the same.

      http://learn-automation.com/selenium-advance-activity/

      Reply
  12. Vinod tewatia says

    October 13, 2016 at 4:30 PM

    By.xpath(“.//*[@id=’autosuggest’]/ul/li[2]/a”));
    i do’t get also autosuggst
    plz define .

    Reply
    • Mukesh Otwani says

      October 13, 2016 at 4:38 PM

      Link please?

      Reply
  13. bindu says

    August 31, 2016 at 6:46 AM

    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?

    Reply
    • Mukesh Otwani says

      September 8, 2016 at 12:19 AM

      Hi Bindu,

      This will help https://plus.google.com/+Mukeshotwani/posts/c4EL2owC7xQ

      Reply
  14. Nikhil says

    August 5, 2016 at 12:05 AM

    Right click example for Google doesnt work. It throws error. This is because the google page is changed.

    Reply
    • Mukesh Otwani says

      August 5, 2016 at 10:25 AM

      Yeah try with some stable application

      Reply
  15. Nikhil says

    August 4, 2016 at 2:01 PM

    When I open naukri, my script fails saying Element not found exception.

    Reply
    • Mukesh Otwani says

      August 5, 2016 at 11:37 AM

      Hey Nikhil,

      Kindly provide more info

      Reply
  16. Madhur Bharadwaj says

    July 27, 2016 at 3:24 PM

    Hi Mukesh , I am not able to find the xpath of the autosuggestion , its not working for me .

    Reply
    • Mukesh Otwani says

      July 27, 2016 at 5:28 PM

      For which application you are trying..

      Reply
  17. sathya says

    April 13, 2016 at 5:32 PM

    why did you used sendKeys(Keys.ARROW_DOWN) multiple times in keyword event block???

    Reply
    • Mukesh Otwani says

      April 13, 2016 at 10:47 PM

      Hi Sathya,

      If you want to select third option then you have to press three time arrow down.

      Reply
  18. amit Chaudhary says

    April 9, 2016 at 10:15 PM

    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

    Reply
    • Mukesh Otwani says

      April 10, 2016 at 11:36 AM

      Hi Amit,

      If you have single operation then its ok but when you have multiple operation then build().perform() is must.

      Reply
      • amit says

        February 3, 2017 at 11:13 AM

        Hi Mukesh , thanks for such a nice blog….
        can you give an example of build and perform …..

        Reply
        • Mukesh Otwani says

          February 5, 2017 at 7:05 PM

          Hi Amit,

          Whenever you use Actions class, the build and perform come into picture. Suppose if you are sending multiple keyboard events.

          Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?