【发布时间】: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~ 现实生活中的请求。
似乎无法弄清楚,我错过了什么?有用的链接:
- https://www.selenium.dev/documentation/support_packages/chrome_devtools/
- CPU throttling in chrome via python selenium
- 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'
【问题讨论】:
-
Wiki 已过时,我尚未将更新的内容移至文档。你应该能够从中找出你需要什么:selenium.dev/documentation/support_packages/chrome_devtools
-
这与我上面的链接完全相同 :p 你指的是
execute_cdp或send_cmd电话吗?我无法让它工作。有什么见解吗? -
@titusfortner 我用 selenium 文档中的内容更新了这个问题。
标签: ruby selenium selenium-webdriver selenium-chromedriver