Hi companions, regularly during computerization we have to right snap or setting click a component. Afterward, this activity is followed up by squeezing the UP/DOWN bolt keys and ENTER key to choose the ideal setting menu component (check our instructional exercise on squeezing the non-content keys in selenium – Pressing ARROW KEYS, FUNCTION KEYS and other non-content keys in Selenium).
For right clicking a component in Selenium, we utilize the Actions class. The Actions class gave by Selenium Webdriver is utilized to create complex client motions including right snap, double tap, intuitive and so on.
Actions action = new Actions(driver);
WebElement element = driver.findElement(By.id("elementId"));
action.contextClick(element).perform();
Here, we are launching an object of Actions class. From that point onward, we pass the WebElement to be correct clicked as parameter to the contestClick() technique present in the Actions class. At that point, we call the perform() strategy to play out the produced activity.
Sample code to right click an element
For the showing of the correct snap activity, we will dispatch Sample Site for Selenium Learning. At that point we will right-click a textbox after which it's setting menu will get shown, affirming that correct snap activity is effectively performed.
package seleniumTutorials;
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 RightClick {
public static void main(String[] args) throws InterruptedException{
WebDriver driver = new FirefoxDriver();
//Launching Sample site
driver.get("https://artoftesting.com/samplesiteforselenium");
//Right click in the TextBox
Actions action = new Actions(driver);
WebElement searchBox = driver.findElement(By.id("fname"));
action.contextClick(searchBox).perform();
//Thread.sleep just for user to notice the event
Thread.sleep(3000);
//Closing the driver instance
driver.quit();
}
}