1

I've managed to locate the correct element on the web page and I can click it. I see the download prompt come up, asking me whether I should open the pdf, open it in Firefox, or save it.

My code looks like this:

my $profile = Selenium::Firefox::Profile->new;
$profile->set_preference(
    "browser.download.dir" => "/Users/john/Downloads",
    "browser.download.folderList" => "1",
    "browser.helperapps.neverAsk.SaveToDisk" => "application/pdf"
);
my $driver = Selenium::Firefox->new(
    'binary' => "/usr/local/bin/geckodriver",
    'firefox_profile' => $profile
);

[...]

$driver->find_child_element($driver->find_element_by_id('secHead_1'), "./a[\@class='phoDirFont']")->click();

My understanding is that if I've set up the correct preferences, then this file would save without the prompt. That's not happening though. I've dug down into it with dev tools, and it does seem to be serving up the pdf file with "application/pdf" as the mime-type. Firefox certainly recognizes it as one (offering to open it in Firefox, not just with the registered app).

If there is another method (perhaps by sending keystrokes to the prompts), that would be acceptable too. Though I've been using Firefox (trying to get away from Google products in my personal life), if Chrome would make a difference I could switch to that instead.

Looking at my about:config (with the Selenium-opened window), the preferences settings seem to have taken. However, it still prompts for the file.

John O
  • 4,863
  • 8
  • 45
  • 78
  • Does the ```a``` element have an attribute ```target="_blank"```? – Piotr M. Jan 05 '21 at 09:01
  • Does this answer your question? [Downloading file through Selenium Webdriver in python](https://stackoverflow.com/questions/44342352/downloading-file-through-selenium-webdriver-in-python) – HedgeHog Jan 05 '21 at 09:31
  • @PiotrM. The a element uses href="javascript:", unfortunately. Part of the reason why I'm not using WWW::Mechanize. – John O Jan 05 '21 at 15:13
  • @HedgeHog That one gave me a few more preferences to try to set, but I seem to be having the same trouble as the other guy... despite those being set (and I'm able to confirm they are from about:config), it still prompts me. – John O Jan 05 '21 at 15:15
  • Could you provide the source code of this html element? – Piotr M. Jan 06 '21 at 11:34

1 Answers1

1

According to https://www.selenium.dev/documentation/en/worst_practices/file_downloads/ file downloads are not supported in selenium.

You could try using curl instead eg:

system("curl -s -o output.pdf -L $URL");

where -s stands for silent, -o stands for where to save the file and -L tells curl to follow redirect If you need cookies you can obtain them with:

my @cookies = $driver->get_all_cookies();

extract whichever cookies you need and then pass them to curl with a --cookie parameter, like so:

system("curl -s -o output.pdf -L --cookie $cookie_one --cookie $another_cookie $URL");
M-K
  • 96
  • 1
  • 8