【问题标题】:OpenQA.Selenium Error: 'invalid argument: 'handle' must be a stringOpenQA.Selenium 错误:'无效参数:'handle' 必须是字符串
【发布时间】:2020-02-17 09:55:03
【问题描述】:

在使用 Selenium 执行远程测试时,我在切换到新窗口时遇到了一些问题。 我在本地测试时没有问题,但是在远程测试时,它不断出现“句柄”必须是字符串错误。我检查了我的代码并确保我的窗口参数是一个字符串。有人请帮忙。

下面是代码和生成的错误。

    public static void SwitchToLoginWindow(IWebDriver webDriver)
    {
        // Wait for the popup to appear
        ReadOnlyCollection<string> wh;

        int timeCount = 1;
        do
        {
            wh = webDriver.WindowHandles;
            Thread.Sleep(200);
            timeCount++;
            if (timeCount > 50)
            {
                break;
            }
        } 

        while (wh.Count == 1);

        //Thread.Sleep(500);
        //int numberOfWindows = wh.Count;
        var numberOfWindow = wh.Count;
        Console.WriteLine($"Switching to Azure AD login popup. Return URL is {webDriver.Url}");
        Thread.Sleep(200);

        webDriver.SwitchTo().Window(wh[numberOfWindow - 1]);
        Waiters.ExplicitWait(webDriver, SelectorType.CssSelector, AzureAd.UserNameField);
    }

异常错误

扔在这里:

webDriver.SwitchTo().Window(wh[numberOfWindow - 1]);

OpenQA.Selenium.WebDriverException: 'invalid argument: 'handle' must be a string (会话信息:chrome=75.0.3770.90) 构建信息:版本:'3.141.59',修订:'e82be7d358',时间:'2018-11-14T08:25:53' 系统信息:主机:'8f5340ba4bc2',ip:'172.21.0.12',os.name:'Linux',os.arch:'amd64',os.version:'4.4.0-145-generic',java.version :'1.8.0_212' 驱动信息:driver.version: unknown'

【问题讨论】:

  • 我遇到了同样的问题,但仅限于 Edge 浏览器。这个问题不是 100% 可重现的,所有浏览器都运行良好,Edge 大约 80% 失败,但有相同的例外。
  • 我在 Edge 中遇到了同样的问题。有人找到解决方案了吗?

标签: c# selenium-webdriver selenium-chromedriver


【解决方案1】:

我遇到了同样的问题,下面的代码帮助我摆脱了错误。初始化 chrome 驱动时必须这样做:

    public IWebDriver GetRemoteChromeDriver(string downloadPath)
    {
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.AddUserProfilePreference("download.default_directory", downloadPath); //just setting he download path
        chromeOptions.AddArgument("--start-maximized");
        chromeOptions.AddArgument("no-sandbox"); //this line is here due another issue, so it isn't necessary
        chromeOptions.AddAdditionalCapability("w3c", false); //this is the relevant line that hopefully will solve the problem
        Environment.SetEnvironmentVariable("webdriver.chrome.driver", @"c:\BuildTools\Selenium\chromedriver.exe"); //
        ICapabilities capability = chromeOptions.ToCapabilities();

        IWebDriver driver = new RemoteWebDriver(capability);

        return driver;
    }

这是最新版 chrome 的错误。它现在默认支持 W3C 规范,无需传递该选项。如果通过了,就会出现这个bug。

我在 github 上的 this 主题上找到了这个解决方案,它为我完成了工作,我希望它也对其他人有所帮助。

【讨论】:

    【解决方案2】:

    '无效参数:'handle' 必须是字符串 我遇到了这个 Selenium 问题,并通过从 Selenium 版本 3.141.59 更改为 3.5.2 暂时解决了这个问题

    【讨论】:

      【解决方案3】:

      对于用户在使用 selenium 3(特别是 3.14)的 Chrome 版本 93 上执行 Browserstack 测试时遇到此错误,我的问题通过添加 browserstack.use_w3c : true 得到解决

      默认情况下,Browserstack 在非 w3c 模式下运行测试。如果我们想启用 w3c 规范,我们需要显式传递它。

      注意:在使用上述功能设置 w3c 规范格式时,请确保您的其他功能符合 w3c。否则,这些将被忽略,并且在创建 webdriver 会话时仅保留符合 w3c 的那些。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-13
        • 1970-01-01
        • 2020-12-01
        • 1970-01-01
        • 2020-06-09
        • 1970-01-01
        • 2023-04-09
        • 1970-01-01
        相关资源
        最近更新 更多