【问题标题】:Using Selenium 2's IWebDriver to interact with elements on the page使用 Selenium 2 的 IWebDriver 与页面上的元素进行交互
【发布时间】:2011-01-11 12:12:15
【问题描述】:

我正在使用 Selenium 的 IWebDriver 在 C# 中编写单元测试。

这是一个例子:

IWebDriver defaultDriver = new InternetExplorerDriver();
var ddl = driver.FindElements(By.TagName("select"));

最后一行检索包裹在IWebElement 中的select HTML 元素。

需要一种方法来模拟选择该 select 列表中的特定 option,但我不知道该怎么做。


在一些research 上,我发现人们使用ISelenium DefaultSelenium 类来完成以下操作的示例,但我没有使用这个类,因为我正在使用IWebDriverINavigation 做所有事情(来自defaultDriver.Navigate())。

我还注意到ISelenium DefaultSelenium 包含大量在IWebDriver 的具体实现中不可用的其他方法。

那么有什么方法可以将IWebDriverINavigationISelenium DefaultSelenium 结合使用?

【问题讨论】:

    标签: c# unit-testing selenium selenium-iedriver


    【解决方案1】:

    正如 ZloiAdun 提到的,在 OpenQA.Selenium.Support.UI 命名空间中有一个可爱的新 Select 类。这是访问选择元素及其选项的最佳方式之一,因为它的 api 非常简单。假设您有一个看起来像这样的网页

    <!DOCTYPE html>
    <head>
    <title>Disposable Page</title>
    </head>
        <body >
            <select id="select">
              <option value="volvo">Volvo</option>
              <option value="saab">Saab</option>
              <option value="mercedes">Mercedes</option>
              <option value="audi">Audi</option>
            </select>
        </body>
    </html>
    

    您访问选择的代码如下所示。请注意我如何通过将普通 IWebElement 传递给它的构造函数来创建 Select 对象。 Select 对象上有很多方法。 Take a look at the source 了解更多信息,直到它被正确记录。

    using OpenQA.Selenium.Support.UI;
    using OpenQA.Selenium;
    using System.Collections.Generic;
    using OpenQA.Selenium.IE;
    
    namespace Selenium2
    {
        class SelectExample
        {
            public static void Main(string[] args)
            {
                IWebDriver driver = new InternetExplorerDriver();
                driver.Navigate().GoToUrl("www.example.com");
    
                //note how here's i'm passing in a normal IWebElement to the Select
                // constructor
                Select select = new Select(driver.FindElement(By.Id("select")));
                IList<IWebElement> options = select.GetOptions();
                foreach (IWebElement option in options)
                {
                    System.Console.WriteLine(option.Text);
                }
                select.SelectByValue("audi");
    
                //This is only here so you have time to read the output and 
                System.Console.ReadLine();
                driver.Quit();
    
            }
        }
    }
    

    然而,关于 Support 类有几点需要注意。即使您下载了最新的测试版,支持 DLL 也不存在。 Support 包在 Java 库(PageObject 所在的地方)中有相当长的历史,但它在 .Net 驱动程序中仍然很新鲜。幸运的是,从源代码构建非常容易。我pulled from SVN 然后从 beta 下载中引用了 WebDriver.Common.dll 并在 C# Express 2008 中构建。这个类没有像其他一些类那样经过很好的测试,但我的示例在 Internet Explorer 和 Firefox 中工作。

    根据您上面的代码,我应该指出其他一些事情。首先是您用来查找选择元素的行

    driver.FindElements(By.TagName("select"));
    

    将找到所有选择元素。你应该使用driver.FindElement,不带's'。

    另外,您很少会直接使用 INavigation。您将完成大部分导航操作,例如 driver.Navigate().GoToUrl("http://example.com");

    最后,DefaultSelenium 是访问旧版 Selenium 1.x api 的方法。 Selenium 2 与 Selenium 1 有很大的不同,因此除非您尝试将旧测试迁移到新的 Selenium 2 api(通常称为 WebDriver api),否则您不会使用 DefaultSelenium。

    【讨论】:

    • +1 由 ZloiAdun 链接的 Select 类完成了这项工作,但我接受了这个答案,因为您提供了更多信息。由于Select 在公共 dll 中尚不可用,我目前正在使用 ZloiAdun 链接的类和异常。至于FindElements,我需要使用那个,因为我想要检索的select 不止1 个。最后,感谢您提到关于DefaultSelenium 的问题,尽管我对此有一个后续问题;有没有办法利用DefaultSelenium 提供的大量方法?
    • +1 用于阐明 IWebDriver 是 2.x 的方式。刚开始,我能找到的大多数演示都使用 DefaultSelenium。
    【解决方案2】:

    您应该使用ddl.FindElements(By.TagName("option")); 从您的select 中获取所有option 元素。然后您可以使用IWebElementSetSelected 方法遍历返回的集合并选择所需的项目

    更新:现在 WebDriver 中似乎有 Select 的 C# 实现 - 以前它仅在 Java 中。请看一下它的code,这个类比较好用

    【讨论】:

    • IWebElement 没有方法 SetSelected。有一种方法叫做Select,但就我而言,它什么也没做。
    • 我没有使用 C#,所以我可能会错过方法名称 - 抱歉。您是否尝试过使用 OpenQA.Selenium.Support.UI.Select 类?您是否在 select 的选项上使用 Select()?
    猜你喜欢
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 2022-10-04
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    • 2022-07-01
    相关资源
    最近更新 更多