如何使用Selenium WebDriver从dojo组合框中获取所选值(2)

时间:2015-05-20 06:16:55

标签: java selenium combobox dojo

如何使用selenium webdriver 2从 dogo comobox 获取列表?

在这个链接中,是dojo como box的一个例子 Dojo Comobox Example

注意:dojo comobox没有id,所以很难找到元素。

我试过了:

        WebDriver driver = new FirefoxDriver();
        driver.get("http://dojotoolkit.org/reference-guide/1.10/dijit/form/ComboBox.html");
        driver.findElement(By.xpath("//*[@id=\"docs_MiniGlass_0\"]/a[1]")).click();     //click on run

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        try {
            Thread.sleep(10*1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        WebElement downArrow = driver.findElement(By.xpath("//*[@id=\"widget_stateSelect\"]/div[1]"));  //to get the arrow
        downArrow.click();


List<WebElement> elements= driver.findElements(By.className("dijitReset.dijitMenu.dijitComboBoxMenu"));

2 个答案:

答案 0 :(得分:0)

<select data-dojo-type="dijit/form/ComboBox" id="fruit" name="fruit">
    <option>Apples</option>
    <option selected>Oranges</option>
    <option>Pears</option>
</select>

您可以直接获取这样的值,而不是获取网络元素列表:

driver.findElement(By.xpath("//select[@data-dojo-type='dijit/form/ComboBox']//option[@selected]")).Text; //not sure if Text attribute is the same in the Java bindings. You may need to review the XPath as well. 

答案 1 :(得分:0)

我以丑陋的方式解决它:

WebElement elem;
try {
    while(true){    //get all web elements
        elem= driver.findElement(By.id("dijit_form_FilteringSelect_0_popup"+i));        //get the dropdown element
        String inner= elem.getAttribute("innerHTML");           //get the text value of the select
        if(inner.equals("mytag")){
            elem.click();
            break;
        }               
        i++;
    }
} catch (Exception e) {
    System.out.println("Finish to find all the dropdown elements");
}

以优雅的方式:

List<WebElement> labels = driver.findElements(By.cssSelector("div[class='dijitReset dijitMenuItem']")); //list of the words