如何在WebDriver中双击并右键单击?

时间:2011-06-21 13:46:42

标签: selenium webdriver

作为项目的一部分,我尝试使用Selenium 2进行自动化。我在下面遇到了麻烦

  1. 如何使用Selenium双击网页元素?

  2. 如何右键点击网页元素从弹出的菜单中选择一个项目?

2 个答案:

答案 0 :(得分:4)

  1. 双击元素有两种方法:

    • 使用DefaultActionSequenceBuilder

      IActionSequenceBuilder action = new
      DefaultActionSequenceBuilder(driver);
      action.DoubleClick(element).Build().Perform();
      
    • 或使用WebDriverBackedSelenium

      ISelenium selenium=new WebDriverBackedSelenium(driver, driver.Url); 
      selenium.Start();
      selenium.DoubleClick("xpath=" + some_xpath);// you could use id, name, etc.
      
  2. ISelenium界面中有ContextMenu方法可用于模拟右键单击。例如:

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

答案 1 :(得分:0)

双击

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

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

右键单击

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

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

如果您想要在执行右键单击后打开的弹出窗口中显示第二个选项,则可以使用以下代码

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