如何从下拉列表中选择项目,并选择类名称

时间:2016-03-16 07:09:49

标签: selenium selenium-webdriver

如何使用Selenium WebDriver从DHTML网格中的下拉列表中选择值?

enter image description here

双击UMO专栏,会看到一个下拉列表。

我需要从该下拉列表中选择一个项目。但定位器无法找到Drop-Down。

我能够双击该字段,之后无法继续(即,Control无法找到下拉列表)。

通过Firebug,它显示为“选择类”..

在selenium webdriver中是否有任何解决方法可以从该下拉列表中选择项目??

如果你对此分享想法,将会非常有帮助。

3 个答案:

答案 0 :(得分:1)

new SelectElement(driver.FindElement(By.ClassName("<className>"))).SelectByIndex("");

new SelectElement(driver.FindElement(By.ClassName("<className>"))).SelectByText("");

 new SelectElement(driver.FindElement(By.ClassName("<className>"))).SelectByValue("");

答案 1 :(得分:0)

以下是可能有用的示例代码。从WebElement创建Select Class的实例。

    public static void main(String[] args){
    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select");
    driver.switchTo().frame(driver.findElement(By.id("iframeResult")));
    WebElement elem = driver.findElement(By.tagName("select"));
    Select se=new Select(elem);
    se.selectByIndex(3);
    try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    driver .close();

}

答案 2 :(得分:0)

WebElement元素= driver.findElements(By.xpath(“ // * [@ id = \” institutionName \“] / option”));

Select select = new Select(element);

    select.selectByVisibleText("value");

    //select.selectByIndex(0);

    //select.selectByValue("1");
相关问题