开发者

How to doubleclick and rightclick in WebDriver?

开发者 https://www.devze.com 2023-03-14 15:12 出处:网络
As part of project I am trying to use Selenium 2 for automation. I am facing trouble with the below How do I double click on a 开发者_运维百科web element using Selenium?

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?


  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.
    


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消