Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
817 views
by
I am working on a scenario where I have to right click on the webpage and perform the operation. How do I do that using Selenium?

4 Answers

2 like 0 dislike
by Expert (570 points)
edited by

You can use below code to right click in Seleium Webdriver:

Actions action = new Actions(driver);
WebElement element = driver.findElement(By.id("elementId"));
action.contextClick(element).perform();

Many professional software testing companies use this simple trick and I hope this will help you too.

1 like 0 dislike
by Master (1.2k points)
You can use contextClick from actions classes.

Actions action= new Actions(driver);
action.contextClick(productLink).build().perform();

Hope that helps!
1 like 0 dislike
by Contributing Tester (54 points)
Hi user,

Let me explain the code here.....

If you want to right click on any element then selenium doesn't provide any direct method for that. We need to use Actions class provided by WebDriver. The Actions class provided by Selenium Webdriver is used to generate complex user gestures including right click, double click, drag and drop etc.
Here is the code:-

Actions action = new Actions(driver);
WebElement element = driver.findElement(By.id("elementId"));
action.contextClick(element).perform();

Here, we are instantiating an object of Actions class. After that, we pass the WebElement to be right clicked as parameter to the contestClick() method present in the Actions class. Then, we call the perform() method to perform the generated action.

Hope this will help you...
0 like 0 dislike
by

1. Using Robot Class :

Robot r= new Robot();

 r.mousePress(InputEvent.BUTTON1_DOWN_MASK); // press right click

r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); // release lright click

2.  Actions action = new Actions(d);

WebElement e1 = driver.findElement(By.id("ID"));
action.contextClick(e1).build().perform();

 


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

...