hound
hound copied to clipboard
File download
I'm opening a new issue over #121 as it is closed without a definitive solution for my case.
I have a site when pressing a download button, the js do window.open(url, "new_window") which opens another window and then the returned response for the new window is a file download which trigger the browser file saving dialog. The driver I use is either chromedriver or selenium with firefox/chrome.
Is there a way to capture and download that file through Hound?
What is the problem exactly when you try to download the file? I got the following to work with chromedriver without any issue:
iex(1)> Hound.start_session
"3c2ec7f8e9342055509a37b88a450ee0"
iex(2)> use Hound.Helpers
Hound.Helpers
iex(3)> navigate_to "https://github.com/HashNuke/hound"
nil
iex(4)> click({:css, ".file-navigation .select-menu-button"})
nil
iex(5)> click({:css, ".get-repo-btn:last-child"})
nil
# file got downloaded inside the default download directory
If the issue is that it is opening a file dialog, for chromedriver, you should be able to save the file directly without triggering the dialog by passing the following in chromeOptions:
chromeOptions: %{
"download" => %{
"prompt_for_download" => false,
"directory_upgrade" => true,
"default_directory" => "/path/to/download/dir"
}
let me know how this works for you.
@tuvistavie And how do you use chromeOptions?? For example this:
Hound.start_session(
driver: %{
chromeOptions: %{ "download" => %{
"prompt_for_download" => true,
"directory_upgrade" => true,
"default_directory" => System.cwd
}}
})
and many other combinations I tried the last 10 hours don't work. Accurate and thorough documentation about these options is absent. Actually, I wasn't able to find any mention about these, except in some discussed issues.
Any help would be greatly appreciated.
@tuvistavie I found it. It is something like this:
Hound.start_session(driver: %{
browserName: "chrome",
chromeOptions: %{
# "args" => ["--headless", "--disable-gpu"],
"prefs" => %{ "download" => %{
"prompt_for_download" => true,
"directory_upgrade" => true,
"default_directory" => System.cwd
}}
}}
)
Has anyone got this to work with headless yet? have tried every single suggestion so far
def chrome_headless_session(metadata) do
{:ok, cwd} = File.cwd()
downloads_path = Path.join(cwd, "test_downloads")
Hound.start_session(
additional_capabilities: %{
chromeOptions: %{
"args" => [
"--user-agent=#{:chrome |> Browser.user_agent() |> Metadata.append(metadata)}",
"--headless",
"--disable-gpu"
],
"prefs" => %{ "download" => %{
"prompt_for_download" => false,
"directory_upgrade" => true,
"default_directory" => downloads_path
}}
}
}
)
end
I got this to work for me. Hope it helps someone down the road.
Has anyone got this to work with headless yet? have tried every single suggestion so far
@fantypants By default, file downloading in headless mode (Chrome, in this case) is prohibited. Try this workaround to enable it:
https://fahrinh.github.io/post/2019-09-16-file-downloading-in-headless-chrome-using-chromedriver-and-hound/