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;
}

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

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

时间: 2024-07-30 13:38:01

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

python selenium中iframe切换、window切换方法

一.selenium中iframe切换方法: 方法一:switch_to.frame frame函数中提供了三种定位方法: driver.switch_to.frame('frame_name') driver.switch_to.frame(1) driver.switch_to.frame(driver.find_elements_by_tag_name("iframe")[0]) 以腾讯课堂为例: from selenium import webdriver from time

用HTML的select+option标签实现下拉框

10.4.2 HTML option 标签 option 标签 -- select菜单中的一个选项 option标签是成对出现的,以<option>开始,以</option>结束 引用网址:http://www.dreamdu.com/xhtml/tag_option/ 属性. common -- 公共属性 disabled -- 输入域无法获得焦点.无法选择,以灰色显示,在表单中不起任何作用 label -- 选择项的短标签 value -- 选项的初始值,未设置时为option

Django 模板中 变量 过滤器 标签 的使用方法

一.变量 1.变量的形式是:{{variable}}, 当模板引擎碰到变量的时候,引擎使用变量的值代替变量.    2.使用dot(.)能够访问变量的属性    3.当模板引擎碰到dot的时候,查找的顺序是什么样子呢?        a.字典查找,例如:foo["var1"]        b.属性查找,例如:foo.bar        c.方法查找,例如:foo.bar()        d.list-index查找,例如foo[bar] 注意:方法查找比一般的查找要复杂一些 (1

HTML中的meta标签及其使用方法

组成 meta标签共有两个属性,分别是http-equiv属性和name属性. 1. name属性 name属性主要用于描述网页,比如网页的关键词,叙述等.与之对应的属性值为content,content中的内容是对name填入类型的具体描述,便于搜索引擎抓取.meta标签中name属性语法格式是: <meta name="参数" content="具体的描述">. 其中name属性共有以下几种参数.(A-C为常用属性) A. keywords(关键字)

Linq中Select查询参数提取公共方法

class Program { static void Main(string[] args) { var listTest1 = new List<Test1> { new Test1{Key="1",Name="1"}, new Test1{Key="2",Name="2"}, new Test1{Key="3",Name="3"} }; var listTest2 =

selenium中WebElement.getText()为空解决方法

当使用getText()获取一个普通的链接文本时: Html代码   <a href="http://www.baidu.com">baidu</a> 如果得到的文本只为空,而非我们期望的baidu.那么尝试使用WebElement.isDisplayed()时候,将会得到false的结果.再尝试使用getAttribute("href"),发现能够争取获取href的值.由此可以说明: WebDriver判定isDisplayed为false

html select与option标签

1.select 元素 select 元素可创建单选或多选菜单.<select> 元素中的 <option> 标签用于定义列表中的可用选项 常用属性如下: autofocus:规定在页面加载后文本区域自动获得焦点. disabled:规定禁用该下拉列表. form:规定文本区域所属的一个或多个表单. multiple:规定可选择多个选项. name:规定下拉列表的名称. required:规定文本区域是必填的. size:规定下拉列表中可见选项的数目. 2.option 元素 op

HTML &lt;select&gt; 标签&lt;option&gt; 标签

HTML <select> 标签 定义和用法 select 元素可创建单选或多选菜单. <select&> 元素中的 <option> 标签用于定义列表中的可用选项. 提示和注释: 提示:select 元素是一种表单控件,可用于在表单中接受用户输入. 属性 New: HTML5 中的新属性. HTML <option> 标签 定义和用法 option 元素定义下拉列表中的一个选项(一个条目). 浏览器将 <option> 标签中的内容作为

div模拟select/option解决兼容性问题及增加可拓展性

想到做这个模拟的原因是之前使用select>option标签的时候发现没有办法操控option的很多样式,比如line-height等,还会由此导致在IE8及以下版本浏览器中的各种问题. 这个模拟思路很简单,也很清晰,我就直接上代码了 html: 第一层div是模拟select标签,第二层div是模拟option下拉列表 1 <div> 2 <div class="sim-select"></div> 3 </div> 4 <