转载

C# Selenium中Select-option标签的定位方法总结

定位页面上的select-option结构,尝试了以下几种方法,均没有生效:

 //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(); 

最后通过以下这种方式,成功定位到了页面上的Select-option标签:

 var selector = iw.FindElement(By.Id("")); SelectElement select = new SelectElement(selector); select.SelectByText("text"); 

Select对象可以通过option的value,text等属性对元素进行定位。

实际应用,如在SharePoint中创建site时,对于site template的选取和site path的选择:

 public IWebDriver CreateSPSite(IWebDriver iw, string caUrl,string title,string template) {     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; } 

对应在页面上的操作,第一段灰色部分的代码对如下标签进行了选择:

C# Selenium中Select-option标签的定位方法总结

第二段灰色部分代码对如下标签进行了选择:

C# Selenium中Select-option标签的定位方法总结

正文到此结束
Loading...