Eduard Florinescu
Eduard Florinescu

Reputation: 17521

Selenium: How can I get the link for downloads that started automatically?

I am using selenium to automate some downloading tasks, but some pages start the downloads automatically and I need to know the links of those downloads during or after the download and the link is not available in the page.

How can I get the URL of the files that started downloading automatically?

Upvotes: 2

Views: 2768

Answers (2)

MAINBRAIN
MAINBRAIN

Reputation: 1

This is a solution I would suggest:

  1. Navigate to chrome://downloads
  2. Use JavascriptExecutor to get a particular download item. The following command gets the first (latest) download item for Chrome Version 125.0.6422.142:
document.getElementsByTagName("downloads-manager")[0]
 .shadowRoot.children["mainContainer"]
 .getElementsByTagName("downloads-item")[0]
 .shadowRoot.getElementById("content")

After having the item you can get the name of the downloaded file and the download link or access the file icon and the closing x For example, extending the command above by

.getElementById("file-link").text will give you the filename.

Upvotes: 0

debugger89
debugger89

Reputation: 2766

If you are using Chrome to run your selenium scripts you can just navigate to the chrome://downloads/ and get the latest download file's URL.

Or you can fire a Ctrl+J keyboard action to the browser using java robot classes and open the tab.

(Chrome downloads page is a html page and you can capture elements in it)

Upvotes: 2

Related Questions