[Selenium With C# 基础教程] Lesson08-下拉列表

常见的下拉列表框如下图所示:

[Selenium With C# 基础教程] Lesson08-下拉列表_第1张图片
8-2 下拉列表示例_c2i.jpg

HTML源码如下:


Selenium中SelectElement类结构

[Selenium With C# 基础教程] Lesson08-下拉列表_第2张图片
8-1 SelectElement Class.jpg

使用SelectElement类需引用命名空间OpenQA.Selenium.Support.UI 在标准的Selenium中,选择下拉列表的类是Select(如Java),但在C#中Select是关键字。

通过文本选择列表

通过该方式仅能选中在浏览器中看到的选项。

IWebElement selectElem = driver.FindElement(By.Name("China"));
SelectElement selectObj = new SelectElement(selectElem);
selectObj.SelectByText("浦东新区");

在使用SelectElement需要引用命名OpenQA.Selenium.Support.UI 。在标准的Selenium中,选择下拉列表的类是Select(比如Java),但在C#中Select是关键字,所以才换了一个名字。

通过Value值选择列表

IWebElement selectElem = driver.FindElement(By.Name("China"));
SelectElement selectObj = new SelectElement(selectElem);
selectObj.SelectByValue("pdditrict");

通过Index选择列表

有些时候,我们并不关心所选择的列表是否为我们所要选择的列表,只需要确定有列表被选中即可,这时我们可以使用通过Index来选择,特别是一些动态生成下拉列表的情况下,使用Index来选择是最好的方式。代码如下所示:

IWebElement selectElem = driver.FindElement(By.Name("China"));
SelectElement selectObj = new SelectElement(selectElem);
selectObj.SelectByIndex(1);

通过循环选择列表

虽然在Selenium中,使用上面几种方法已经可以实现选择下拉列表了。在实际情况如果需要循环选择某些选项,该怎么写脚本了?详细代码如下:

IWebElement selectElem = driver.FindElement(By.Name("China"));
ReadOnlyCollection options = driver.FindElements(By.TagName("option"));
//循环每个列表选中一次
for (int i = 0; i < options.Count; i++)
{
    options[i].Click();
}
//循环选中一个列表
for (int i = 0; i < options.Count; i++)
{
    if (options[i].Text.Contains("徐汇"))
    {
        options[i].Click();
    }
}

选择多个列表选项

下拉列表中不仅支持单选也支持多选,多选下拉列表示例如下所示:

[Selenium With C# 基础教程] Lesson08-下拉列表_第3张图片
8-3 下列列表多选_c2i.jpg

HTML源码如下:

请选择你向往的城市:

示例代码如下:

IWebElement cityEle = driver.FindElement(By.Id("city"));
SelectElement citySelect = new SelectElement(cityEle);
citySelect.SelectByText("上海");
citySelect.SelectByValue("wuhan");
citySelect.SelectByIndex(6);
Assert.AreEqual(3,citySelect.AllSelectedOptions.Count);

最终的运行效果如下所示:

[Selenium With C# 基础教程] Lesson08-下拉列表_第4张图片
8-4 多选运行效果_c2i.jpg

清除单个选中列表

清除所有选择列表只需要调用SelectElement类的方法DeselectByTextDeselectByValueDeselectByIndex方法即可

IWebElement cityEle = driver.FindElement(By.Id("city"));
SelectElement citySelect = new SelectElement(cityEle);
citySelect.SelectByText("上海");
citySelect.SelectByValue("wuhan");
citySelect.SelectByIndex(6);
Assert.AreEqual(3,citySelect.AllSelectedOptions.Count);
citySelect.DeselectByText("上海");
citySelect.DeselectByValue("wuhan");
citySelect.DeselectByIndex(6);
Assert.AreEqual(0, citySelect.AllSelectedOptions.Count);

清除所有选择列表

清除所有选择列表只需要调用SelectElement类的方法DeselectAll方法即可。

IWebElement cityEle = driver.FindElement(By.Id("city"));
SelectElement citySelect = new SelectElement(cityEle);
citySelect.SelectByText("上海");
citySelect.SelectByValue("wuhan");
citySelect.SelectByIndex(6);
Assert.AreEqual(3,citySelect.AllSelectedOptions.Count);
citySelect.DeselectAll();
Assert.AreEqual(0, citySelect.AllSelectedOptions.Count);

通过选择项断言

下拉列表中断言可以通过SelectElement类中SelectedOption方法来断言选择的列表是否正确,代码如下:

IWebElement selectElem = driver.FindElement(By.Name("China"));
SelectElement selectObj = new SelectElement(selectElem);
selectObj.SelectByText("浦东新区");
Assert.AreEqual("浦东新区", selectObj.SelectedOption.Text);

通过下拉列表Value属性断言

IWebElement selectElem = driver.FindElement(By.Name("China"));
SelectElement selectObj = new SelectElement(selectElem);
selectObj.SelectByText("浦东新区");
Assert.AreEqual("pdditrict", selectObj.SelectedOption.GetAttribute("value"));

多选下拉列表断言

因多选下拉列表可允许用户选中多个选项,那么我们可以将用户选中的项做为一个集合来对待,然后进行断言,代码如下:

IWebElement cityEle = driver.FindElement(By.Id("city"));
SelectElement citySelect = new SelectElement(cityEle);
citySelect.SelectByText("上海");
citySelect.SelectByValue("wuhan");
citySelect.SelectByIndex(6);

IList selected = citySelect.AllSelectedOptions;
Assert.AreEqual(3, selected.Count);
Assert.AreEqual("上海",selected[0].Text);

本文同步在微信订阅号上发布,如各位小伙伴们喜欢我的文章,也可以关注我的微信订阅号:woaitest,或扫描下面的二维码添加关注:

[Selenium With C# 基础教程] Lesson08-下拉列表_第5张图片
MyQRCode.jpg

你可能感兴趣的:([Selenium With C# 基础教程] Lesson08-下拉列表)