Selenium WebDriver - get options from hidden select

后端 未结 2 1309
长发绾君心
长发绾君心 2021-01-14 04:00

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


                        
    
提交评论

  • 2021-01-14 04:26

    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.

    0 讨论(0)
  • 提交回复
    热议问题