gpt4 book ai didi

selenium - 如何在chromedriver中关闭w3c以解决错误unknown command : Cannot call non W3C standard command while in W3C

转载 作者:行者123 更新时间:2023-12-02 02:56:58 27 4
gpt4 key购买 nike

随着 Chrome 75 版本的发布,我们的测试不再正常运行。他们给出了粘贴在下面的堆栈跟踪。我们使用 ruby​​ on Rails v. 5.1.6.2 和 rspec、selenium-webdriver 3.8.0。

堆栈跟踪:

Selenium::WebDriver::Error::UnknownCommandError:
unknown command: Cannot call non W3C standard command while in W3C mode
# 0 chromedriver 0x000000010c46e8e9 chromedriver + 3594473
# 1 chromedriver 0x000000010c3fe543 chromedriver + 3134787
# 2 chromedriver 0x000000010c1aa29f chromedriver + 692895
# 3 chromedriver 0x000000010c11a691 chromedriver + 104081
# 4 chromedriver 0x000000010c11b7d5 chromedriver + 108501
# 5 chromedriver 0x000000010c42d555 chromedriver + 3327317
# 6 chromedriver 0x000000010c438e60 chromedriver + 3374688
# 7 chromedriver 0x000000010c438bf8 chromedriver + 3374072
# 8 chromedriver 0x000000010c40cd39 chromedriver + 3194169
# 9 chromedriver 0x000000010c4396d8 chromedriver + 3376856
# 10 chromedriver 0x000000010c420f27 chromedriver + 3276583
# 11 chromedriver 0x000000010c456064 chromedriver + 3493988
# 12 chromedriver 0x000000010c474617 chromedriver + 3618327
# 13 libsystem_pthread.dylib 0x00007fff7744c2eb _pthread_body + 126
# 14 libsystem_pthread.dylib 0x00007fff7744f249 _pthread_start + 66
# 15 libsystem_pthread.dylib 0x00007fff7744b40d thread_start + 13
# /Users/julie/.rvm/gems/ruby-2.5.1/gems/selenium-webdriver-3.8.0/lib/selenium/webdriver/remote/response.rb:69:in `assert_ok'
# /Users/julie/.rvm/gems/ruby-2.5.1/gems/selenium-webdriver-3.8.0/lib/selenium/webdriver/remote/response.rb:32:in `initialize'
# /Users/julie/.rvm/gems/ruby-2.5.1/gems/selenium-webdriver-3.8.0/lib/selenium/webdriver/remote/http/common.rb:81:in `new'
# /Users/julie/.rvm/gems/ruby-2.5.1/gems/selenium-webdriver-3.8.0/lib/selenium/webdriver/remote/http/common.rb:81:in `create_response'
# /Users/julie/.rvm/gems/ruby-2.5.1/gems/selenium-webdriver-3.8.0/lib/selenium/webdriver/remote/http/default.rb:104:in `request'
# /Users/julie/.rvm/gems/ruby-2.5.1/gems/selenium-webdriver-3.8.0/lib/selenium/webdriver/remote/http/common.rb:59:in `call'
# /Users/julie/.rvm/gems/ruby-2.5.1/gems/selenium-webdriver-3.8.0/lib/selenium/webdriver/remote/bridge.rb:166:in `execute'
# /Users/julie/.rvm/gems/ruby-2.5.1/gems/selenium-webdriver-3.8.0/lib/selenium/webdriver/remote/oss/bridge.rb:579:in `execute'
# /Users/julie/.rvm/gems/ruby-2.5.1/gems/selenium-webdriver-3.8.0/lib/selenium/webdriver/remote/oss/bridge.rb:526:in `element_displayed?'
# /Users/julie/.rvm/gems/ruby-2.5.1/gems/selenium-webdriver-3.8.0/lib/selenium/webdriver/common/element.rb:199:in `displayed?'
# /Users/julie/.rvm/gems/ruby-2.5.1/gems/capybara-2.17.0/lib/capybara/selenium/node.rb:148:in `visible?'
# /Users/julie/.rvm/gems/ruby-2.5.1/gems/capybara-2.17.0/lib/capybara/node/element.rb:269:in `block in visible?'
# /Users/julie/.rvm/gems/ruby-2.5.1/gems/capybara-2.17.0/lib/capybara/node/base.rb:81:in `synchronize'
# /Users/julie/.rvm/gems/ruby-2.5.1/gems/capybara-2.17.0/lib/capybara/node/element.rb:269:in `visible?'
# /Users/julie/.rvm/gems/ruby-2.5.1/gems/capybara-2.17.0/lib/capybara/queries/selector_query.rb:84:in `matches_filters?'
# /Users/julie/.rvm/gems/ruby-2.5.1/gems/capybara-2.17.0/lib/capybara/result.rb:29:in `block in initialize'

我们的驱动程序配置:

File.write(LOG_FILE_PATH, '')
Selenium::WebDriver.logger.level = :debug
Selenium::WebDriver.logger.output = LOG_FILE_PATH
Capybara.register_driver :selenium do |app|
# from https://github.com/SeleniumHQ/selenium/issues/3738
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(loggingPrefs: {browser: 'ALL'})
options = Selenium::WebDriver::Chrome::Options.new
options.add_argument '--disable-infobars' # hide info bar about chrome automating test
# if we don't use this flag, every selenium test will die with the error:
# "unknown error: Chrome failed to start: exited abnormally"
options.add_argument '--no-sandbox'
options.add_argument '--headless' if ENV.fetch("HEADLESS", nil).present?
options.add_argument '--window-size=1600,2400'
options.add_argument '-–allow-file-access-from-files' # TODO Julie - may help with file specs?
options.add_preference('homepage', 'about:blank') # TODO is this working?
options.add_preference('profile.default_content_settings.popups', 0)
options.add_preference('download.default_directory', DownloadHelpers::PATH.to_s)
Capybara::Selenium::Driver.new(
app,
clear_local_storage: true,
clear_session_storage: true,
browser: :chrome,
options: options,
desired_capabilities: capabilities,
)
end

更新:

我能够使用 capabilities = { "chromeOptions" => {'w3c' => false} } 让我们的测试暂时运行。

更新 chromedriver 后,我们开始收到错误“未知错误:DevToolsActivePort 文件不存在”。为了解决这个问题,我们将 selenium-webdriver gem 升级到 3.142.3,这解决了这个问题,允许我们在没有任何额外参数的情况下使用 w3c。

最佳答案

首先是解决方案

正如 John Chen [所有者 - Google Chrome WebDriver] 昨天所 promise 的那样,新版本的 ChromeDriver 75.0.3770.9076.0.3809.25已发布,现已在 ChromeDriver Downloads 站点上提供。这些版本包含与之前版本的 ChromeDriver 7576 相比的以下错误修复:

  • 修复了 OSS 模式下错误拒绝正文为空的 POST 请求的错误
  • 添加了用于检索 Chrome 日志的新端点

此外,版本76.0.3809.25还包括以下更改:

  • 在 W3C 模式下添加了 Is Displayed 命令的端点
<小时/>

电子邮件快照

75_76

<小时/>

详细信息

在 chromedriver 中关闭 w3c 来解决错误将违反最佳实践:

Selenium::WebDriver::Error::UnknownCommandError:
unknown command: Cannot call non W3C standard command while in W3C mode

由于 ChromeDriver 的当前实现向客户端请求符合 W3C 的 session 。

<小时/>

但是,此错误消息意味着 ChromeDriver 在启动/生成时无法在 W3C 模式 下调用非 W3C 标准命令一个新的 WebBrowser,即 Chrome 浏览器 session 。

主要问题是,当 ChromeDriver 的客户端请求 W3C 兼容 session 时,ChromeDriver 的响应不符合W3C 规范,并导致语言 API 出现错误。

根据 ChromeDriver response in W3C mode is not standard compliant John Chen(Google Chrome 所有者 - WebDriver)mentioned Simon Stewart(创建者 - WebDriver)中的讨论已更新:

  • w3c session 的新 session 响应应如下所示:

    {
    "value": {
    "sessionId": "some-uuid",
    "capabilities": {
    "browserName": "chrome",
    ...
    }
    }
    }
  • 但是,当在 w3c 选项设置为 true 的情况下启动新 session 时,< em>chromeOptions,返回的响应如下:

        {
    "sessionId": "af4656c27fb94485b7872e1fc616923a",
    "status": "ok",
    "value": {
    "browserName": "chrome",
    ...
    }
    }

这既不是 JSON Wire Protocol 的正确形成的响应(其中“状态”将为整数),也不是正确形成的 W3C 响应,如果没有正确形成的响应,则无法使用 w3c 兼容。

这个 revision 和这个 commit 解决了这个问题。

<小时/>

这个用例

大概您正在将 ChromeDriver v75.xChrome v75.x 结合使用,如果您仍然看到该错误,则需要传递 ExperimentalOption w3c 作为 true 明确如下:

  • Ruby 代码示例:

    capabilities = { "chromeOptions" => {'w3c' => true} }
  • Java代码示例:

    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;

    public class W3c {
    public static void main(String[] args) throws Exception {
    ChromeOptions opt = new ChromeOptions();
    opt.setExperimentalOption("w3c", true);
    ChromeDriver driver = new ChromeDriver(opt);
    driver.get("https://www.google.co.in");
    }
    }
  • Python代码示例:

    from selenium import webdriver

    opt = webdriver.ChromeOptions()
    opt.add_experimental_option('w3c', True)
    driver = webdriver.Chrome(chrome_options=opt)
    driver.get('https://www.google.co.in')
<小时/>

更新

直到 ChromeDriver v74.x 为止,ChromeChromeDriver 组合默认在 w3c 模式下运行,但 chromedriver/server/http_handler.cc 中存在错误。根据 goog:chromeOptions.w3c=false doesn't work for POST request with empty body 中的详细信息:

Method HttpHandler::HandleCommand checks the value of the kW3CDefault constant instead of session goog:chromeOptions.w3c value. As a result, JSON Wire protocol support was broken, where POST requests with an empty body are allowed. JSON Wire protocol will be in demand until displayed endpoint is resumed in the w3c mode. It should be noted that W3C WebDriver specification doesn't forbid the use of 'displayed' endpoint and this feature is actively used in some APIs.

As Is Element Displayed 命令不是 W3C 规范的一部分,但仍被某些 API 使用,并且其功能很难在这些 API 中复制。此更改列表 [revisioncommit] 在 W3C 模式下重新启用此命令,以轻松过渡到 W3C 模式。

@John 已确认我们预计明天将更新 ChromeDriver v75.0 并修复该问题。

关于selenium - 如何在chromedriver中关闭w3c以解决错误unknown command : Cannot call non W3C standard command while in W3C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57070093/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com