Selenium WebDriver - get options from hidden select

China☆狼群 提交于 2019-12-01 06:37:40

问题


I want to get all the options from a hidden select. Select has "display: none;" part so I ran into a problem.

<select name="fw3k_ad_input_et_type_group" 
            class="" id="_id_fw3k_ad_input_et_type_group" 
            onchange=" eurotax.change_type_group( this.value ); " 
            style="display: none; ">

        <option value="0">1</option>
        <option value="-1" class="special">2</option>
        <option value="16390">CD</option>
        <option value="17605">S</option>
        <option value="17636">SE</option>

</select>

My code:

Select tipSelect = new Select(driver.findElement(By.name("fw3k_ad_input_et_type_group")));
for (WebElement b : tipSelect.getOptions()) {
            System.out.println(b.getText());
}

Please display code example if You have any.

firebug view: http://imageshack.us/f/138/primjer.png/ LOOK THIS


回答1:


It is possible to select elements in firebug with "display: none;" attribute. They will not be outlined on the page, but in the html tree structure.

Then, verify that you found element propertly with firebug

String optn=select[name="fw3k_ad_input_et_type_group"] option[value="0"]
//optn1=select[name="fw3k_ad_input_et_type_group"] option[value="-1"]
//optn2=select[name="fw3k_ad_input_et_type_group"] option[value="16390"]
//optn3=select[name="fw3k_ad_input_et_type_group"] option[value="17605"]
//optn4=select[name="fw3k_ad_input_et_type_group"] option[value="17636"]

then try to use jscript executor (should always work not taking into consideration whether element visible or not)

JavascriptExecutor js = (JavascriptExecutor) driver;
        StringBuilder stringBuilder = new StringBuilder();

stringBuilder.append("var x = $(\""+optn+"\");");
        stringBuilder.append("return x.text().toString();")       ;


       String res= (String) js.executeScript(stringBuilder.toString());

Hope this work for you)




回答2:


The matter is selenium unable to click invisible elements (or interact with invisible elements in other ways). So js should help. I would resolve it in the following way:

String css1="ul>li:last-child>div[id=unos_select_wrap] select[id="_id_fw3k_ad_input_et_type_group"]>option[value='0']";
String css2="ul>li:last-child>div[id=unos_select_wrap] select[id="_id_fw3k_ad_input_et_type_group"]>option[value='-1']";
String css3="ul>li:last-child>div[id=unos_select_wrap] select[id="_id_fw3k_ad_input_et_type_group"]>option[value='16390']";
String css4="ul>li:last-child>div[id=unos_select_wrap] select[id="_id_fw3k_ad_input_et_type_group"]>option[value='17605']";
String css5="ul>li:last-child>div[id=unos_select_wrap] select[id="_id_fw3k_ad_input_et_type_group"]>option[value='17636']";

public void getOptionTextAndPrintIt(String cssSelector){
JavascriptExecutor js = (JavascriptExecutor) driver;
        StringBuilder stringBuilder = new StringBuilder();

stringBuilder.append("var x = $(\""+cssSelector+"\");");
        stringBuilder.append("return x.text().toString();");
       String res= (String) js.executeScript(stringBuilder.toString());
    System.out.println(res);

}
public void allOptionValuesDepiction(){
getOptionTextAndPrintIt(css1);
getOptionTextAndPrintIt(css2);
getOptionTextAndPrintIt(css3);
getOptionTextAndPrintIt(css4);
getOptionTextAndPrintIt(css5);
}

Please let me know if something wrong as soon as you check.



来源:https://stackoverflow.com/questions/12345403/selenium-webdriver-get-options-from-hidden-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!