Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get the downloaded file name? (Selenium)

HTML code:

<a target="_blank" class="truncate-text" id="rfq-display-attachment-0">
          BEHR SDS.pdf
        </a>

I am downloading this link:

link1 = driver.find_element_by_xpath("//a[@id='rfq-display-attachment-0']")
filename = link1.text
link1.click()

According to this, the filename will be BEHR SDS.pdf But after downloading the file, the filename of the downloaded file is BEHR SDS.1637344008787.pdf. Here the name in the text and the actual filenames are different.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How to get the accurate filename in this case? I don’t want the text of the <a> tag?

>Solution :

Based on OP comment above that,

I think trimming the .1637344008787 part is enough since every file name has this type of part only.

You can do the following:

a = "BEHR SDS.1637344008787.pdf"
orginal_file_name = a.split('.')[0] + '.pdf'
print(orginal_file_name)

Now I have hardcoded the file name, you should go to directory and look for the latest file which has been downloaded.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading