selenium webdriver - Select an element from drop-down list using java -
i wondering how select element after found in ddl (drop-down list). function can find element failed select it; tried click didn't work.
thanks
code is:
public static void selectguestcountry() { webelement droplist = driver.findelement(by.classname("sboptions")); list<webelement> alloptions = droplist.findelements(by.tagname("a")); system.out.println("total options in list --> " + alloptions.size()); (int i=0;i<alloptions.size();i++ ) { //system.out.println("++> " + alloptions.get(i).getattribute("text")); if(alloptions.get(i).getattribute("text").equals("france")) { system.out.println("++++>" + alloptions.get(i).getattribute("text")); **alloptions.get(i).click();**// doesn't select country break; } } ---------------------------------------------------------------
here html code, thanks:
<div class="guestform"> <input id="reservations0.guests0.id" type="hidden" value="0" name="reservations[0].guests[0].id"> <br> <div class="form-border"> <h5> <div class="field-col left"> <div class="field-col right"> <div> <div class="emails"> <div class="contactinfo"> <div class="field-row"> <label class="ellipsis" title="organization">organization</label> <input id="reservations0.guests0.organization" class="field" type="text" maxlength="40" value="" name="reservations[0].guests[0].organization"> </div> <div class="field-row select-big country"> <label> <select id="reservations0.guests0.address.country.alpha2code" class="selectbox" onchange="populatestates(this);" name="reservations[0].guests[0].address.country.alpha2code" sb="73833889" style="display: none;"> <div id="sbholder_73833889" class="sbholder"> <a id="sbtoggle_73833889" class="sbtoggle" href="#"></a> <a id="sbselector_73833889" class="sbselector" href="#">select country</a> <ul id="sboptions_73833889" class="sboptions" style="display: none;"> <li> <li> <a href="#us" rel="us">united states</a> </li> <li> <a href="#af" rel="af">afghanistan</a> </li> <li> <li> <li>`
few minor tweaks code, please read below if acceptable :
use select object instead of webelement find ddl object, can use selectbyindex (easier per code) select element list.