简体   繁体   English

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

[英]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? 如何使用Selenium Webdriver 2从dogo comobox获取列表?

In this link ther is an example of dojo como box Dojo Comobox Example 在此链接中,这是dojo como框的示例Dojo Comobox示例

Note: dojo comobox don't have an id, so it hard to find elements. 注意:dojo comobox没有ID,因此很难找到元素。

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: 无需获取Web元素列表,您可以直接使用以下值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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