OpenQA.Selenium错误:'无效的参数:'句柄'必须是字符串

时间:2019-10-21 10:11:05

标签: c# selenium-webdriver selenium-chromedriver

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

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

    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:'无效的参数:'handle'必须是字符串     (会议信息:chrome = 75.0.3770.90)   构建信息:版本:'3.141.59',修订版本:'e82be7d358',时间:'2018-11-14T08:25:53'   系统信息:主机:'8f5340ba4bc2',ip:'172.21.0.12',操作系统名称:'Linux',os.arch:'amd64',os.version:'4.4.0-145-generic',java.version :'1.8.0_212'   驱动程序信息:driver.version:未知'

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,下面的代码帮助我摆脱了错误。在初始化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规范,而无需通过该选项。如果通过,则会发生此错误。

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