How to double-click and right-click in WebDriver?

As part of the project, I am trying to use Selenium 2 for automation. I ran into a problem with below

  • How to double click on a web element using Selenium?

  • How should I right-click a web item to select an item from the menu?

+4
source share
2 answers
  • There are two ways to double-click an item:

    • using DefaultActionSequenceBuilder class

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

       ISelenium selenium=new WebDriverBackedSelenium(driver, driver.Url); selenium.Start(); selenium.DoubleClick("xpath=" + some_xpath);// you could use id, name, etc. 
  • There is a ContextMenu method in the ISelenium interface that you can use to simulate a right-click. For instance:

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

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 need the second version of the popup that opens after right-clicking, you can use the code below

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

All Articles