如何使用webdriver单击IE中的选择选项?

时间:2012-10-16 19:13:23

标签: selenium webdriver selenium-webdriver

private void select(WebDriver driver, String select_text) {
    System.out.println("Selecting "+select_text+" from drop down menu");
    Select select = new Select(driver.findElement(By.name("roomMenu")));
    select.selectByVisibleText(select_text);
}

此功能适用于firefox,但在IE中运行时,它不会点击任何选项。有没有一种特定的方式我必须为IE做这个?

编辑:
我在不使用Select对象的情况下重写了它,但它仍然拒绝单击该选项。

private void select(WebDriver driver, String select_text) {
    System.out.println("Selecting "+select_text+" from drop down menu");

    WebElement select = driver.findElement(By.name("roomMenu"));
    List<WebElement> options = select.findElements(By.tagName("option"));

    for (WebElement option : options) {
        if (option.getText().equals(select_text)) {
            System.out.println(option.getText());
            option.click();
        }
    }
}

它打印出正确的选项,所以我知道它找到了正确的选项,但是当我执行option.click()时,IE中没有任何反应。

1 个答案:

答案 0 :(得分:1)

我用 -

private boolean selectFromDropDown(String locator, String value) {
    try {
        new Select(driver.findElement(By.xpath(locator))).selectByVisibleText(value);
        return true;
    }
     catch (Exception e) {
            verificationErrors.append(e.toString());
            System.out.println("Could not find element");
            return false;
        }
}

在IE中也可以正常工作! 从here获得。