【问题标题】:Throttle CPU in chromedriver with selenium使用硒的 chromedriver 中的节流 CPU
【发布时间】:2021-12-13 20:29:48
【问题描述】:

所以我尝试在下面的selenium chromedriver 设置中添加CPUThrottlingRate

require 'webdrivers/chromedriver'

module ChromeBrowser
  class << self

    def headless(throttling)
      @browser ||= headless_chrome_browser(throttling || 'regular')
    end

    private

    def headless_chrome_browser(throttling)
      options = Selenium::WebDriver::Chrome::Options.new

      # -> https://peter.sh/experiments/chromium-command-line-switches/#profiler-timing
      options.add_argument '--ignore-certificate-errors'
      # options.add_argument "--user-agent=#{random_user_agent}" # set Random User Agent
      options.add_argument '--allow-insecure-localhost'
      options.add_argument '--window-size=1400x1400'
      options.add_argument '--disable-gpu'
      options.add_argument '--disable-dev-shm-usage'
      options.add_argument '--noerrdialogs' # Suppresses all error dialogs when present
      options.add_argument '--profiler-timing=0' # whether chrome will contain timing information
      options.add_argument '--disable-infobars' # prevent infobars from appearing
      options.add_argument '--headless' if Rails.env.production? # headless on chrome
      # options.add_argument '--blink-settings=imagesEnabled=false' # disable images
      options.add_argument '--no-referrers' # don't send HTTP-Referer headers
      options.add_argument '--disable-breakpad' # disables the crash reporting
      options.add_argument '--disable-demo-model' # disables the chrome OS demo
      options.add_argument '--disable-translate' # disable google translate
      options.add_argument '--dns-prefetch-disable' # disable DNS prefetching
      options.add_argument '--no-pings' # no hyperlink auditing pings

      browser = Selenium::WebDriver.for :chrome, options: options

      case throttling
      when 'slow'
        browser.network_conditions = {
          offline: false,
          latency: 50,
          download_throughput: 51200,
          upload_throughput: 51200
        }
      when 'regular'
        browser.network_conditions = {
          offline: false,
          latency: 30,
          download_throughput: 51200,
          upload_throughput: 512000
        }
      when 'fast'
        browser.network_conditions = {
          offline: false,
          latency: 20,
          download_throughput: 1024000,
          upload_throughput: 1024000
        }
      end

      return browser
    end
  end
end

我在这里寻找 this 解决方案,但使用 ruby​​。

## rate 1 is no throttle, 2 is 2x slower, etc. 
driver.execute_cdp_cmd("Emulation.setCPUThrottlingRate", {'rate': 10})
  • developertools 已安装
  • running on chromedriver v92

我正在尝试同时限制网络速度和 CPU 以模拟 ~kinda~ 现实生活中的请求。

似乎无法弄清楚,我错过了什么?有用的链接:


我试过了:

...
browser = Selenium::WebDriver.for :chrome, options: options
devToolsSession = browser.devtools
devToolsSession.send_cmd('Emulation.setCPUThrottlingRate', {rate: 10})

哪个产生

ArgumentError: wrong number of arguments (given 2, expected 1)
from /Users/minijohn/.asdf/installs/ruby/3.0.2/lib/ruby/gems/3.0.0/gems/selenium-webdriver-4.0.3/lib/selenium/webdriver/devtools.rb:55:in `send_cmd'

只有一个参数

Selenium::WebDriver::Error::WebDriverError: -32602: Invalid parameters: Failed to deserialize params.rate - BINDINGS: mandatory field missing at position 7
from /Users/minijohn/.asdf/installs/ruby/3.0.2/lib/ruby/gems/3.0.0/gems/selenium-webdriver-4.0.3/lib/selenium/webdriver/devtools.rb:69:in `send_cmd'

也试过了

browser = Selenium::WebDriver.for :chrome, options: options
browser.send_cmd('Emulation.setCPUThrottlingRate', {rate: 10})

产生

NoMethodError: undefined method `send_cmd' for #<Selenium::WebDriver::Chrome::Driver:0x2738339e1fe77884 browser=:chrome>
from (pry):14:in `headless_chrome_browser'

【问题讨论】:

  • Wiki 已过时,我尚未将更新的内容移至文档。你应该能够从中找出你需要什么:selenium.dev/documentation/support_packages/chrome_devtools
  • 这与我上面的链接完全相同 :p 你指的是execute_cdpsend_cmd 电话吗?我无法让它工作。有什么见解吗?
  • @titusfortner 我用 selenium 文档中的内容更新了这个问题。

标签: ruby selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

Selenium 的目标是定义浏览器供应商将在 WebDriver BiDi 规范中支持的常用命令,并通过简单的 API 支持它们,该 API 可通过 Driver#devtools 方法访问。

同时,任何 Chrome DevTools 命令都可以通过Driver#execute_cdp 执行。

在这种情况下,它看起来像:

SL-1495:code titusfortner$ irb
irb(main):001:0> require 'selenium-webdriver'
=> true
irb(main):002:0> driver = Selenium::WebDriver.for :chrome
=> #<Selenium::WebDriver::Chrome::Driver:0x..fc42365c4d30079b0 browser=:chrome>
irb(main):003:0> driver.execute_cdp('Emulation.setCPUThrottlingRate', rate: 10)
=> {}
irb(main):004:0> 

这里的关键是,在 Ruby 中,我们为此使用关键字而不是 Hash,而在 Ruby 3 中,它们现在的处理方式有所不同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-05
    • 2019-06-03
    • 2020-05-16
    • 2014-11-05
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 2017-05-12
    相关资源
    最近更新 更多