In this tutorial, we will learn how to double click and right click in Selenium WebDriver with examples.
Selenium has emerged as a very powerful opensource that is widely being accepted across the world as the first choice of automation tool for automating web-based applications.
In this tutorial, I will explain to you how to perform mouse events like Right Click and Double Click in Selenium Webdriver. While automating an application sometimes we need to perform right-click or double-click on certain elements to accomplish certain tasks. We will use the actions class in Selenium to perform double click and right-click. If you want to know more about actions please refer to my post Actions Class in Selenium WebDriver.
How to Right Click in Selenium Using Action Class?
A right click operation is also called a Context Click. In order to perform a right click in Selenium, we will have to use contextClick() method that requires a WebElement as an argument. The ContextClick() emulates the behavior of doing a right click.
The syntax for doing right click in Selenium is as follows.
Actions actions = new Actions(driver);
WebElement element = driver.findElement(“By any locator”));
actions.contextClick(element).perform();
Example of Doing Right Click in Selenium WebDriver
To do a hands-on to verify right click please copy the following HTML code in notepad and save it with the file name rightclick.html.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
<!DOCTYPE html> <html> <head> <style type="text/css"> .context-menu { position: absolute; text-align: center; background: lightgray; border: 1px solid black; } .context-menu ul { padding: 0px; margin: 0px; min-width: 150px; list-style: none; } .context-menu ul li { padding-bottom: 7px; padding-top: 7px; border: 1px solid black; } .context-menu ul li a { text-decoration: none; color: black; } .context-menu ul li:hover { background: darkgray; } </style> </head> <body> <h1 style="text-align: center;"> Welcome to MySkillPoint. </h1> <h2 style="text-align: center; color: #4CAF50;"> We will Test Right Click.Right Click anywhere on the Page to open a list menu </h2> <div id="contextMenu" class="context-menu" style="display:none"> <ul> <li><a href="https://www.facebook.com/">Click Me To Open Facebook</a></li> <li><a href="https://www.google.com/">Click Me To Open Google</a></li> <li><a href="https://www.instagram.com/">Click Me To Open Instagram</a></li> <li><a href="https://en.wikipedia.org/">Click Me To Open Wikipedia</a></li> </ul> </div> <script> document.onclick = hideMenu; document.oncontextmenu = rightClick; function hideMenu() { document.getElementById( "contextMenu").style.display = "none" } function rightClick(e) { e.preventDefault(); if (document.getElementById( "contextMenu").style.display == "block") hideMenu(); else { var menu = document .getElementById("contextMenu") menu.style.display = 'block'; menu.style.left = e.pageX + "px"; menu.style.top = e.pageY + "px"; } } </script> </body> </html> |
When you will open the page and do a right click it will show a menu like the following screenshot.
We will automate the following test scenario
- Open the link
- Do a right click on the page
- Click on the second menu to open the Google search page.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; public class RightClickExampleInSelenium { public static void main(String[] args) throws InterruptedException { //Setting the path of Chrome Browser Driver String BrowserDriverPath= "C:\\SeleniumBrowserDrivers\\chromedriver.exe"; System.setProperty("webdriver.chrome.driver",BrowserDriverPath); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); driver.get("file:///C:/TestFolder/rightclick.html"); driver.manage().window().maximize(); WebElement body = driver.findElement(By.xpath("/html/body")); WebElement linkGoogle=driver.findElement(By.xpath("//a[text()='Click Me To Open Google']")); //Do a right click on the body of the page Actions actions = new Actions(driver); actions.contextClick(body).perform(); //Switch the the menu option driver.switchTo().activeElement(); linkGoogle.click(); //Uncomment following line to Close the browser //driver.quit(); } } |
How to Double Click in Selenium Using Action Class?
We can do double click using DoubleClick() of actions class.
Example of Doing Double Click in Selenium WebDriver
We can perform a double click using the following syntax.
Actions actions = new Actions(driver);
WebElement element = driver.findElement(“By any locator”));
actions.doubleClick(element).perform();
We will automate the following test scenario
- Open the link https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_ondblclick
- Double click on the text “Double-click this paragraph to trigger a function.”
- It will display a “Hello World” Text message as shown below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.interactions.Actions; public class DoubleClickExampleInSelenium { public static void main(String[] args) throws InterruptedException { //Setting the path of Chrome Browser Driver String BrowserDriverPath= "C:\\SeleniumBrowserDrivers\\chromedriver.exe"; System.setProperty("webdriver.chrome.driver",BrowserDriverPath); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); driver.get("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_ondblclick"); driver.manage().window().maximize(); //Switching to the iframe as the paragraph is inside an iframe driver.switchTo().frame("iframeResult"); WebElement elementText=driver.findElement(By.xpath("/html/body/p[1]")); //Do a right click on the body of the page Actions actions = new Actions(driver); actions.doubleClick(elementText).perform(); System.out.println(driver.findElement(By.xpath("//*[@id=\"demo\"]")).getText()); //Uncomment following line to Close the browser //driver.quit(); } } |
Conclusion
We have learned how to do right click and double click using Actions Class in Selenium Webdriver.Hope you will find this article helpful.If you have any queries please do mention it in the comment box and don’t forget to share this article.
Recommended Posts
- Top #23 Most Useful Selenium WebDriver Commands That You Must know
- How to Download and Install Eclipse
- Configure Selenium Webdriver With Eclipse
- How to Select a Value from a DropDown In Selenium WebDriver And Doing More Validations?
- Wait Commands in Selenium WebDriver | Implicit | Explicit | Fluent Wait
- Handle Dynamic Web Tables In Selenium WebDriver
- How to Handle a New Tab in Selenium WebDriver and Switch Between Tabs
- How to Handle Multiple Windows In Selenium WebDriver
- Page Object Model (POM) & Page Factory in Selenium WebDriver