Home » selenium-tutorial » Double Click and Right Click using selenium

Newly Updated Posts

Double Click and Right Click using selenium

Previous Topic, In this tutorial we will learn how to Double Click and Right Click in selenium,selenium has its pre-defined classes because of which we perform operation like keyboard operations, mouse movements like drag and drop, mouse hover etc.

Double Click and Right click using selenium

How to perform Double click in selenium?

Double click in selenium and Right Click is perforrmed by Action class. In Action class we can perform operation of keyboard and mouse actions. Find below code to double click by action class.

Actions mouseActn=new Actions(driver);						 						  
WebElement locator =driver.findElement(By.id("ID"));						 mouseActn.doubleClick(locator).build().perform();

Description on above code:

  • Firstly we create the object of Action class and pass the driver as a parameter to it
  • Create the locator of the element to perform double click.
  • call the doubleClick method from the instance variable of action class the pass the locator as a parameter along with build().perform();

Consider a scenario to under and perform double click to understand more better

package SeleniumArchitecture;
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 FiestTestScriptChrome {
public Boolean Flag =false;
public static void main(String[] args) throws InterruptedException  {
//System property of the Chrome driver 
			  System.setProperty("webdriver.chrome.driver","C:\\Users\\XXXXXX\\Desktop\\Eclipse_Installer\\GeckoDriver\\chromedriver.exe");
 // Create oject of chrome driver 
 WebDriver driver =  new ChromeDriver();
Thread.sleep(3000);			driver.get("http://demo.guru99.com/test/simple_context_menu.html");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS);
WebElement clickBouble =driver.findElement(By.xpath("//*[@id=\"authentication\"]/button"));
 Actions mouseActn=new Actions(driver);
Thread.sleep(3000);
mouseActn.doubleClick(clickBouble).build().perform();
 Thread.sleep(3000);		  
driver.quit();
}

}

Right Click in Selenium

Right Click in selenium can be performed by Action Class in selenium.The Right Click in selenium is called as context Click. The syntax for context click in selenium is below:

Actions mouseActn=new Actions(driver);
WebElement clickContext =driver.findElement(By.id("ID"));
 Thread.sleep(3000);						 mouseActn.contextClick(clickContext).build().perform();

Description of above code is below:

  • Firstly we create the object of Action class and pass the driver as a parameter to it
  • Create the locator for the element to right click .
  • call the contextClick method from the instance variable of action class the pass the locator as a parameter along with build().perform();

Consider a scenario to under and perform right click to understand more better :

package SeleniumArchitecture;
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 FiestTestScriptChrome {

public static void main(String[] args) throws InterruptedException  {
//System property of the Chrome driver			  System.setProperty("webdriver.chrome.driver","C:\\Users\\XXXX\\Desktop\\Eclipse_Installer\\GeckoDriver\\chromedriver.exe");
// Create oject of chrome driver 
 WebDriver driver =  new ChromeDriver();
 Thread.sleep(3000);						  driver.get("http://demo.guru99.com/test/simple_context_menu.html");
 driver.manage().window().maximize();
 driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS);
Actions mouseActn=new Actions(driver);
 WebElement clickContext =driver.findElement(By.xpath("//*[@id=\"authentication\"]/span"));
Thread.sleep(3000);						  mouseActn.contextClick(clickContext).build().perform();
 Thread.sleep(3000);		  
 driver.quit();			  
	}
}

So in this Tutorial we have learned how to double Click and right click in selenium webdriver.

If you guys an comment and topics which can be discussed feel free to contact us and write.