码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2015-12-01 12:35:27      阅读:304      评论:0      收藏:0      [点我收藏+]

标签:

定位页面上的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标签的定位方法总结

标签:

原文地址:http://www.cnblogs.com/LanTianYou/p/5009617.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!