所以我試圖CPUThrottlingRate在selenium chromedriver下面的設定中添加。
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
我在這里尋找這個解決方案,但在 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 來模擬~有點~現實生活中的請求。
似乎無法弄清楚,我錯過了什么?有用的鏈接:
- https://www.selenium.dev/documentation/support_packages/chrome_devtools/
- 通過python selenium在chrome中節流CPU
- https://github.com/SeleniumHQ/selenium/wiki/Ruby-Bindings
我試過了:
...
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'
uj5u.com熱心網友回復:
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 中,現在以不同的方式處理它們。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/340879.html
