作为项目的一部分,我尝试使用Selenium 2进行自动化。我在下面遇到了麻烦
如何使用Selenium双击网页元素?
如何右键点击网页元素从弹出的菜单中选择一个项目?
答案 0 :(得分:4)
双击元素有两种方法:
使用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.
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();