I am looking to streamline the process of downloading multiple files from a website. To achieve this, I navigate through several pages to obtain the file IDs using selenium and a perl script.
Since selenium does not have a built-in download function and using curl is not an option due to needing the session information from selenium, I am attempting to implement a workaround that I have successfully used in other projects.
The workaround involves creating a JavaScript element and executing a script with the URL for each file, like so:
var a = document.createElement("a");
a.setAttribute("href", "https://myurl.com/id1/export?format=TCX");
a.setAttribute("download", "https://myurl.com/id1/export?format=TCX");
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
However, when I attempt this, I encounter the following error in the script:
Error while executing command: stale element reference: The element reference of is stale; either the element is no longer attached to the DOM, it is not in the current frame context, or the document has been refreshed at /Library/Perl/5.18/Selenium/Remote/Driver.pm line 391. at /Library/Perl/5.18/Selenium/Remote/Driver.pm line 348.
Testing this directly in the Firefox console yields:
TransitionRejection(type: 2, message: The transition has been superseded by a different transition, detail: Transition#2( 'top.ids.info.byId'{"myId1":"XXXXX","myId2":"YYYYYY"} -> 'home'{"feedId":null,"tag":null} ))
When accessing the URL via a normal browser GET request, I receive the download prompt without any issues (provided I am logged in).
Using selenium to access the URL works for the first file, but then gets stuck and does not proceed to download the second file.
$driver->get($download_url1); # This one is downloaded
$driver->get($download_url2); # This line is not executed