To locate the Select-option structure on the page, try the following methods, none of them are effective:
IW. Findelement (By.cssselector ("Select#id > Option[value=")). Click (); //IW. Findelement (By.xpath ("//select[@id = ']/option[@value = ']")). Click (); //IW. Findelements (by.tagname ("option")) [index]. Click ();
Finally, the Select-option label on the page was successfully located in the following way:
var selector = IW. Findelement (By.id (""new selectelement (selector); Select. Selectbytext ("text");
Select objects can be positioned on an element by using attributes such as option Value,text.
Practical applications, such as when creating a site in SharePoint, for site template selection and site path choice:
Public Iwebdriver Createspsite (Iwebdriver IW,String Caurl,String title,StringTemplate) {String createsitepage = Caurl +"/_admin/createsite.aspx"; Iw. Navigate (). Gotourl (Createsitepage); Waituntilpageloadedid (IW,"Ctl00_placeholdermain_ctl02_rptcontrols_btncreatesite");//Title. Iw. Findelement (By.id ("Ctl00_placeholdermain_idtitledescsection_ctl01_txtcreatesitetitle")). SendKeys (title);//Url.var paths = IW. Findelement (By.id ("ctl00_placeholdermain_ctl01_ctl04_ddlwildcardinclusion")); SelectElement Path = new selectelement (paths); Path. Selectbytext ("/sites/"); Waituntilpageloadedid (IW,"Ctl00_placeholdermain_ctl01_ctl04_txtsitename"); Iw. Findelement (By.id ("Ctl00_placeholdermain_ctl01_ctl04_txtsitename")). SendKeys (title);Switch(template) {Case"Ts":Break;Case"Edi": IW. Findelement (By.id ("Ctl00_placeholdermain_inputformtemplatepickercontrol_ctl00_ctl01_tabid1")). Click (); Thread.Sleep (1000); Waituntilpageloadedid (IW,"Ctl00_placeholdermain_inputformtemplatepickercontrol_ctl00_ctl01_lbwebtemplate");var selector = IW. Findelement (By.id ("ctl00_placeholdermain_inputformtemplatepickercontrol_ctl00_ctl01_lbwebtemplate")); SelectElement select = new SelectElement (selector); Select. Selectbytext ("EDiscovery Center");Break;Case"Rc": IW. Findelement (By.id ("Ctl00_placeholdermain_inputformtemplatepickercontrol_ctl00_ctl01_tabid1")). Click (); Thread.Sleep (1000); Waituntilpageloadedid (IW,"Ctl00_placeholdermain_inputformtemplatepickercontrol_ctl00_ctl01_lbwebtemplate");Selector= IW. Findelement (By.id ("ctl00_placeholdermain_inputformtemplatepickercontrol_ctl00_ctl01_lbwebtemplate")); select = new SelectElement (selector); Select. Selectbytext ("Records Center");Break;Case"Holdcenter": IW. Findelement (By.id ("Ctl00_placeholdermain_inputformtemplatepickercontrol_ctl00_ctl01_tabid1")). Click (); Thread.Sleep (1000); Waituntilpageloadedid (IW,"Ctl00_placeholdermain_inputformtemplatepickercontrol_ctl00_ctl01_lbwebtemplate");Selector= IW. Findelement (By.id ("ctl00_placeholdermain_inputformtemplatepickercontrol_ctl00_ctl01_lbwebtemplate")); select = new SelectElement (selector); Select. Selectbytext ("In-place hold Policy Center");break;} iw. Findelement (By.id ("ctl00_placeholdermain_idprimaryadministratorsection_ctl01_pickerowner_upleveldiv ")). SendKeys (@ "userName"); Iw. Findelement (By.id ("ctl00_placeholdermain_ctl02_rptcontrols_btncreatesite")). Click (); return IW;}
corresponding to the operation on the page, the code for the first gray section selects the following tags:
The second section of the gray Section code selects the following labels:
Summary of positioning methods of select-option tags in C # selenium