Manojith
Manojith

Reputation: 41

How to doubleclick and rightclick in WebDriver?

As part of project I am trying to use Selenium 2 for automation. I am facing trouble with the below

  1. How do I double click on a web element using Selenium?

  2. How should I right click on a web element to select an item from the menu pop up?

Upvotes: 4

Views: 8144

Answers (2)

JtGadara
JtGadara

Reputation: 1

Double Click

WebElement ele = driver.findelement(By.id("id_of_element"));

Actions action = new Actions(driver)
action.doubleClick(ele).perform();

Right Click

WebElement ele = driver.findelement(By.id("id_of_element"));

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

If you want second option on the pop up which opens after performing right click you can use below code

action.contextClick(ele).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).build().perform();

Upvotes: 0

Andrushka
Andrushka

Reputation: 41

  1. There are 2 ways to double click on element:

    • using DefaultActionSequenceBuilder class

      IActionSequenceBuilder action = new
      DefaultActionSequenceBuilder(driver);
      action.DoubleClick(element).Build().Perform();
      
    • or using WebDriverBackedSelenium class

      ISelenium selenium=new WebDriverBackedSelenium(driver, driver.Url); 
      selenium.Start();
      selenium.DoubleClick("xpath=" + some_xpath);// you could use id, name, etc.
      
  2. There is ContextMenu method in ISelenium interface you could use for simulating Right click. For instance:

    ISelenium selenium=new WebDriverBackedSelenium(driver, driver.Url);
    selenium.Start();
    selenium.ContextMenu("xpath=" + some_xpath);// you could use id, name, etc.
    

Upvotes: 4

Related Questions