简体   繁体   中英

How to get the selected value from a dojo combobox using Selenium WebDriver(2)

How can i get the list from dogo comobox using selenium webdriver 2?

In this link ther is an example of dojo como box Dojo Comobox Example

Note: dojo comobox don't have an id, so it hard to find elements.

I tried:

        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"));
<select data-dojo-type="dijit/form/ComboBox" id="fruit" name="fruit">
    <option>Apples</option>
    <option selected>Oranges</option>
    <option>Pears</option>
</select>

Instead of getting list of web elements you can directly go for the value like this:

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. 

I solve it in ugly way:

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");
}

in elegant way:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM