1.

How to perform right-click using WebDriver?

Answer»

In some scenarios, you might NEED to do the right click action/context on an element to perform some actions. Use Selenium WebDriver class Actions to work on Mouse and Keyboard Actions. Selenium has a built-in capability to handle various types of keyboard and mouse events. In order to perform action events, we need to use org.openqa.selenium.interactions Actions class,  user-facing API for performing complex user gestures. Instead of USING Keyboard or Mouse directly, you can use the selenium actions class.

Let us see the scenario to automate

  1. First launch the web browser and open the facebook application
  2. Then find the required element and do right click on the element SAY LOGIN button
package rightclickaction; 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 RightClickAction {          public static void main(String[] ARGS) throws InterruptedException{                      WebDriver driver = new FirefoxDriver();                      //Launching site                      driver.get("http://facebook.com");                      //Right click on the LogIn button                      Actions action = new Actions(driver);                      WebElement loginBox = driver.findElement(By.cssSelector(".#u_0_a"));                      action.contextClick(loginBox).perform();                      //Thread.sleep just to notice event                      Thread.sleep(3000);                      //Closing the driver instance                      driver.quit();          } }


Discussion

No Comment Found