【问题标题】:How to set ImplicitWait in Selenium 3.1.0如何在 Selenium 3.1.0 中设置 ImplicitWait
【发布时间】:2017-02-22 09:34:17
【问题描述】:

升级Selenium WebDriver 3.1.0后我收到警告

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));

警告 11 'OpenQA.Selenium.ITimeouts.SetScriptTimeout(System.TimeSpan)' 已过时:'此方法将在未来版本中删除。请改为设置 AsynchronousJavaScript 属性。'

所以我改成

driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);

ChromeDriver 工作正常,但是当我尝试FireFoxDriver 时,该行的测试失败,抛出异常

结果消息:System.InvalidOperationException:缺少“类型” 参数 (IndexOutOfBounds) 结果 StackTrace: at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(响应 错误响应)在 OpenQA.Selenium.Remote.RemoteWebDriver.Execute(字符串 driverCommandToExecute,Dictionary`2 参数)在 OpenQA.Selenium.Remote.RemoteWebDriver.InternalExecute(字符串 driverCommandToExecute,Dictionary'2 参数)在 OpenQA.Selenium.Remote.RemoteTimeouts.ExecuteSetTimeout(字符串 timeoutType, TimeSpan timeToWait) 在 OpenQA.Selenium.Remote.RemoteTimeouts.set_ImplicitWait(时间跨度值) 在 OpenQA.Selenium.Support.Events.EventFiringWebDriver.EventFiringTimeouts.set_ImplicitWait(TimeSpan 值)

如何解决?

【问题讨论】:

    标签: c# selenium selenium-webdriver selenium-firefoxdriver


    【解决方案1】:

    在挖掘github 中的source code 之后,我发现ImplicitWait setget 方法正在使用(在较低级别)ICommandExecutor 来设置和获取值。唯一在父 RemoteWebDriver 中初始化此变量的 FireFoxDriver 构造函数是

    public FirefoxDriver(FirefoxDriverService service, FirefoxOptions options, TimeSpan commandTimeout) : base(CreateExecutor(service, options, commandTimeout), ConvertOptionsToCapabilities(options))
    

    触发基础构造函数

    public RemoteWebDriver(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
    

    FirefoxDriver 中的方法CreateExecutor(在初始化基时在构造函数中调用)检查firefoxOptions 成员UseLegacyImplementation 并在DriverServiceCommandExecutor 时返回DriverServiceCommandExecutor,这并没有解决问题,和FirefoxDriverCommandExecutortrueUseLegacyImplementation默认为false,所以应该设置为true

    对我有用的解决方案是

    FirefoxOptions firefoxOptions = new FirefoxOptions
    {
        Profile = fxProfile, // I'm using FirefoxProfile as well
        UseLegacyImplementation = true
    };
    
    IWebDriver driver = new FirefoxDriver(FirefoxDriverService.CreateDefaultService(), firefoxOptions, TimeSpan.FromSeconds(30));
    

    【讨论】:

    • 我会指出这个解决方案不适用于 Firefox 48 及更高版本。通过设置UseLegacyImplementation 属性,您选择使用旧的、不再维护的Firefox 驱动程序实现。自动化 Firefox 48 或更高版本需要使用 geckodriver,这是默认设置。
    • 如果您使用的是options.UseLegacyImplementation = true,那么我很抱歉,但是,不,您没有使用 geckodriver。 FirefoxDriver 类的 CreateExecutor 方法显式地在此属性的值上分支,仅当值为 false 时才启动 geckodriver。
    • 那么有没有其他方法可以在没有旧版支持的情况下为 FIrefoxDriver 设置 ImplicitlyWait?与 geckodriver 有同样的例外。刚刚从 Selenium 2.53 迁移到 3.x,由于所有这些丑陋的问题,我真的想回滚到 2.53。
    • @Yauhen.F 通过与同事的讨论,我了解到geckodriver 本身还没有设置ImplicitWait 的实现(应该在较新的版本中修复)。但是我自己没有看到任何官方消息来源,所以我没有在我的回答中包含这个。
    【解决方案2】:

    我在我的项目中遇到了同样的问题。然而,一个短期的解决方案是回滚到以前版本的 webdriver。

    我希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-06
      • 1970-01-01
      • 2018-02-07
      • 1970-01-01
      • 2016-10-25
      • 1970-01-01
      • 2023-03-26
      相关资源
      最近更新 更多