Selenium get last downloaded file name
Simplified solution to get the path to last file in Downloads folder. The above code will get path of the latest. Change the extension as required. Or else you can chose not to specify the extension. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How to get last downloaded file using selenium Ask Question. Asked 6 years, 5 months ago. Active 1 year, 9 months ago. Viewed 14k times. Thanks in advance.
Improve this question. Add a comment. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Who owns this outage? Building intelligent escalation chains for modern SRE. Podcast Who is building clouds for the independent developer? Featured on Meta. Now live: A fully responsive profile. Reducing the weight of our footer. Related Hot Network Questions.
Question feed. Stack Overflow works best with JavaScript enabled. Accept all cookies Customize settings. This is my code: Is there anyway that I can give the file being downloaded a specific name? Why not rename it after the download Using the Shutil module? Since the filname always have a random name, I want to name it when I am downloading it. So it will be easier to locate it later. Add a comment. Active Oldest Votes. Community Bot 1 1 1 silver badge. If yest which package? I need to import it accordingly.
You need the below 3 imports. The chrome entry was helpful, but "except: pass" is a horrible idea. In incognito mode at least on the latest chrome, it spits out an error: selenium.
JavascriptException: Message: javascript error: Cannot read property 'shadowRoot' of null It can't find the percentage tag it seems, and fails with no proper error message due to your overly broad except case.
Thanks for bringing up the behavior in the latest chrome version. There might be changes to the behavior of the browser, I will take a look into this. This is still the most efficient answer for me with a few tweaks though If you want the webdriver to always wait until the download is complete, you can remove the wait time and the if block — kcEmenike. Show 5 more comments. Note: borrowed methods from google searches may have errors.
Sudharsana Rajasekaran 3 3 silver badges 10 10 bronze badges. The problem with above code is that filename var gets its value before the download is finished. This will create two type of errors. If the download directory was empty, then filename will be empty and hence shutil. If the directory was not empty, filename will get the name of last file downloaded before current file which is still downloading and shutil. You need to implement a method to wait for download to finish.
It could be useful to also compare the file with current timestamp datetime. Show 2 more comments. Hi, dmb and Kreuni, I would like to add this snippet into my code with python3. Can you please tell me which modification I would need to do to make it work on macOS?
I ask because I did just that and it does not function, however it does not give me an error either to go with. TilHund Do, os. Thank you, dmb. I added what you have said with the same outcome. No error message or complaint was given, but the two sample jpg files were not modified into 1.
0コメント