Home > Enterprise >  How to get the downloaded file name? (Selenium)
How to get the downloaded file name? (Selenium)

Time:11-23

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.

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

CodePudding user response:

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.

  • Related