Selenium 2 Remote WebDriver 和 C# 出现问题 - 为什么我的字典不起作用?

发布于 2024-11-04 18:43:44 字数 1929 浏览 1 评论 0原文

首先,需要注意的是:我是 C# 的新手,所以如果这是一个极其简单的问题,请原谅我。我正在将一些 Selenium Python 测试转换为 C#,而且我才刚刚开始。

因此,我在一个测试项目中有以下示例,试图让 Selenium 2 在 C# 中工作:

public class Selenium2RemoteWebDriver
{
    static void Main(string[] args)
    {
        var pltfm = new Platform(PlatformType.Windows);

        var environment = new Dictionary<string, object>
        {
            {"username", "my-sauce-name"},
            {"accessKey", "my-sauce-key"},
            {"browserName", "iexplore"},
            {"version", "8"},
            {"platform", "Windows"},
            {"name","Hello, Sauce!"}
        };

        //foreach (var pair in environment)
        //{
        //    Console.WriteLine("Key: {0}, Value: {1}", pair.Key, pair.Value);
        //}


        var capabilities = new DesiredCapabilities(environment);
        var driver = new RemoteWebDriver(
            new Uri("http://my-sauce-id:[email protected]:80/wd/hub"), capabilities);

        driver.Navigate().GoToUrl("http://www.google.com");
        var search = driver.FindElement(By.Name("q"));
        search.SendKeys("Hello, WebDriver");
        search.Submit();

        Console.WriteLine(driver.Title);
        driver.Quit();
    }
}

当我运行此测试时,我收到以下消息:

Test 'T:Selenium2_Testing.Selenium2RemoteWebDriver' failed: The given key was not present in the dictionary.

但是当我取消注释 print 语句时,我可以看到字典已完成:

Key: username, Value: my-sauce-id
Key: accessKey, Value: my-sauce-key
Key: browserName, Value: iexplore
Key: version, Value: 8
Key: platform, Value: Windows
Key: name, Value: Hello, Sauce!

什么我做错了吗?

编辑:抱歉:我应该包括我正在使用的:

Visual Studio 2010 (10.30319.1)
.Net Framework 4.0 (30319)
Selenium WebDriver 2.0b3(已编译的下载)

First, a caveat: I'm brand new to C#, so please forgive me, if this is a ridiculously simple question. I'm converting some Selenium Python tests to C#, and I'm just getting started.

So, I have the following example in a test project, trying to get Selenium 2 working in C#:

public class Selenium2RemoteWebDriver
{
    static void Main(string[] args)
    {
        var pltfm = new Platform(PlatformType.Windows);

        var environment = new Dictionary<string, object>
        {
            {"username", "my-sauce-name"},
            {"accessKey", "my-sauce-key"},
            {"browserName", "iexplore"},
            {"version", "8"},
            {"platform", "Windows"},
            {"name","Hello, Sauce!"}
        };

        //foreach (var pair in environment)
        //{
        //    Console.WriteLine("Key: {0}, Value: {1}", pair.Key, pair.Value);
        //}


        var capabilities = new DesiredCapabilities(environment);
        var driver = new RemoteWebDriver(
            new Uri("http://my-sauce-id:[email protected]:80/wd/hub"), capabilities);

        driver.Navigate().GoToUrl("http://www.google.com");
        var search = driver.FindElement(By.Name("q"));
        search.SendKeys("Hello, WebDriver");
        search.Submit();

        Console.WriteLine(driver.Title);
        driver.Quit();
    }
}

When I run this test, I get the following message:

Test 'T:Selenium2_Testing.Selenium2RemoteWebDriver' failed: The given key was not present in the dictionary.

But when I uncomment the print statements, I can see the dictionary is complete:

Key: username, Value: my-sauce-id
Key: accessKey, Value: my-sauce-key
Key: browserName, Value: iexplore
Key: version, Value: 8
Key: platform, Value: Windows
Key: name, Value: Hello, Sauce!

What am I doing wrong?

EDIT: Apologies: I should have included that I am using:

Visual Studio 2010 (10.30319.1)
.Net Framework 4.0 (30319)
Selenium WebDriver 2.0b3 (the already compiled download)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的笑 2024-11-11 18:43:44

.NET 绑定中的 DesiredCapativity 对象最近(2.0b3 后)被重写,以正确允许传递字典来定义任意功能。行为发生更改的确切代码更改可通过此链接。

The DesiredCapabilities object in the .NET bindings was recently (post 2.0b3) rewritten to correctly allow for passing a dictionary to define arbitary capabilities. The exact code change where the behavior was changed is visible at this link.

长伴 2024-11-11 18:43:44

事实证明我使用的程序集有问题。从最新的源进行编译并将这些二进制文件导入到项目中后,这个问题就消失了。

Turns out there's something wrong with the assemblies I'm using. After compiling from the most recent source, and importing those binaries into the project, this problem ceased.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文