Selenium WebDriver - get options from hidden select

落花浮王杯 提交于 2019-12-01 10:39:08

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)

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.

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