如何在selenium中指定默认下载路径?
我在Docker中使用headless chrome .
# docker-compose.yml
services:
chrome:
container_name: chrome
image: selenium/standalone-chrome-debug:3.9.1-actinium
ports:
- 4444:4444
和我的代码在下面
# ruby
chrome_capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
"chromeOptions" => {
'prefs' => {
'profile.default_content_settings.popups' => 0,
'download.default_directory' => File.absolute_path(@download_path),
'download.prompt_for_download' => false,
}
}
)
driver = Selenium::WebDriver.for(
:remote,
url: "http://chrome:4444/wd/hub",
desired_capabilities: chrome_capabilities,
)
然而,当我在chrome中运行下载时,我得到一个下载提示。
如何自动下载到指定文件夹?
你试试这个代码,然后告诉我。
require 'selenium-webdriver'
client = Selenium::WebDriver::Remote::Http::Default.new
client.read_timeout = 60 # seconds
prefs = {
download: {
'prompt_for_download' => false,
'default_directory' => "c:/foldername"
},
"credentials_enable_service" => false,
"password_manager_enabled" => false
}
options = { prefs: prefs,
options: { "excludeSwitches" => ["enable-automation", "load-extension"] } }
selenium_args = [:chrome, { http_client: client, options: options} ]
@driver = Selenium::WebDriver.for(*selenium_args)