Chrome 使用水豚和 selenium 无头下载 pdf
Chrome headless download pdf using capybara and selenium
我在 Rails (5.2.1) 项目的 Ruby 中使用 chrome headless with Selenium (3.14.0) 和 Capybara (3.8.0),我有一个测试在非无头 chrome 中有效,但在无头 chrome 中无效。我在 google chrome 稳定版本 69 上使用“--headless”标志。
我已经使用以下设置了无头 chrome,这适用于所有不下载文件的测试。
download_path="#{Rails.root}/tmp/downloads"
Capybara.register_driver(:headless_chrome) do |app|
caps = Selenium::WebDriver::Remote::Capabilities.chrome(
chromeOptions: {
prefs: {
'download.default_directory' => download_path,
"download.extensions_to_open" => "applications/pdf",
'download.directory_upgrade' => true,
'download.prompt_for_download' => false,
'plugins.plugins_disabled' => ["Chrome PDF Viewer"]
},
binary: "/opt/google/chrome/google-chrome",
args: %w[headless disable-gpu window-size=1920,1080]
}
)
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
desired_capabilities: caps
)
end
我读到我应该向 selenium chrome 驱动程序发送命令以允许下载,但我不知道如何使用我的设置执行此操作。这是我想要开始工作的,但使用我的设置; (不是来自我的代码库);
@driver = Selenium::WebDriver.for :chrome, options: options
bridge = @driver.send(:bridge)
path = '/session/:session_id/chromium/send_command'
path[':session_id'] = bridge.session_id
bridge.http.call(:post, path, cmd: 'Page.setDownloadBehavior',
params: {
behavior: 'allow',
downloadPath: download_path
})
如何在我的设置中访问 selenium 桥,以便我可以发送此 http 调用?
您不再需要手动发送它,它已作为 Selenium::WebDriver::Chrome::Server#download_path=
添加到 selenium 中。您可以通过 Capybara::Selenium::Driver
实例
在您的驱动程序注册中设置它
...
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
desired_capabilities: caps
).tap { |d| d.browser.download_path = <your download path> }
我在 Rails (5.2.1) 项目的 Ruby 中使用 chrome headless with Selenium (3.14.0) 和 Capybara (3.8.0),我有一个测试在非无头 chrome 中有效,但在无头 chrome 中无效。我在 google chrome 稳定版本 69 上使用“--headless”标志。
我已经使用以下设置了无头 chrome,这适用于所有不下载文件的测试。
download_path="#{Rails.root}/tmp/downloads"
Capybara.register_driver(:headless_chrome) do |app|
caps = Selenium::WebDriver::Remote::Capabilities.chrome(
chromeOptions: {
prefs: {
'download.default_directory' => download_path,
"download.extensions_to_open" => "applications/pdf",
'download.directory_upgrade' => true,
'download.prompt_for_download' => false,
'plugins.plugins_disabled' => ["Chrome PDF Viewer"]
},
binary: "/opt/google/chrome/google-chrome",
args: %w[headless disable-gpu window-size=1920,1080]
}
)
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
desired_capabilities: caps
)
end
我读到我应该向 selenium chrome 驱动程序发送命令以允许下载,但我不知道如何使用我的设置执行此操作。这是我想要开始工作的,但使用我的设置; (不是来自我的代码库);
@driver = Selenium::WebDriver.for :chrome, options: options
bridge = @driver.send(:bridge)
path = '/session/:session_id/chromium/send_command'
path[':session_id'] = bridge.session_id
bridge.http.call(:post, path, cmd: 'Page.setDownloadBehavior',
params: {
behavior: 'allow',
downloadPath: download_path
})
如何在我的设置中访问 selenium 桥,以便我可以发送此 http 调用?
您不再需要手动发送它,它已作为 Selenium::WebDriver::Chrome::Server#download_path=
添加到 selenium 中。您可以通过 Capybara::Selenium::Driver
实例
...
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
desired_capabilities: caps
).tap { |d| d.browser.download_path = <your download path> }