'How download files when href values in their WebElements are a path to the same folder?
I try to automate the downloading of couples of text files. Operating manually, they can be downloaded by clicking on the buttons defined by the 2 following elements.
<a class="btn ...." href="/..../...../....../..../download" data-value="ext1">Download (.ext1) file</a>
<a class="btn ...." href="/..../...../....../..../download" data-value="ext2">Download (.ext2) file</a>
Please note that the two href values are the same and it is a path to a folder.
Using Selenium, I find the 2 FirefoxWebElements by
elem1=driver.find_elements_by_link_text("Download (.ext1) file")
elem2=driver.find_elements_by_link_text("Download (.ext2) file")
I can download the two files by
elem1.send_keys(Keys.ENTER) and
elem2.send_keys(Keys.ENTER),
then I have to find the two files in the download folder and I have to move them to the destination folder.
Note that I do not know the name of these files.
I would prefer to do it by requests to have them directly at their destination folder with the appropriate names.
url1=elem1.get_attribute('href')
r = requests.get(url1, allow_redirects=True)
open(destination1, 'wb').write(r.content)
and
url2=elem2.get_attribute('href')
r = requests.get(url2, allow_redirects=True)
open(destination2, 'wb').write(r.content)
But I get the same file (the first one) in both the cases since href value is the same.
Would it be possible to download the two files directly to their destination folder? (may be additional parameters of requests.get or other ways?)
Solution 1:[1]
You can try tracking the network-tab on your browser when clicking the download-button and see what kind of request is sent. Maybe the request has "ext1" or "ext2" in the payload or something to separate between the files. Sending payload for GET request in python
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | juhat |
