Skip to content

Instantly share code, notes, and snippets.

@ic0n
Last active November 17, 2023 17:53
Show Gist options
  • Save ic0n/a38b354cac213e5aa50c55a0d8b87a0b to your computer and use it in GitHub Desktop.
Save ic0n/a38b354cac213e5aa50c55a0d8b87a0b to your computer and use it in GitHub Desktop.
Get chromedriver current downloads progress(es) with python using selenium

Notice:

* This method is tested on Chrome Version 61.0.3163.100, chromedriver Version 2.33.506106

Getting the progress of the first downloads-item

So for some reason, chrome keeps download items in the shadow-root of tag .

This makes me so confused when I am trying to automate chrome to download files.

After some research, i find out that you can access the elements inside shadow-root in javascript with property .shadowRoot of the DOM element which the ShadowRoot is attached.

And this is the code:

from selenium import webdriver

def get_download_progress():
    progress = driver.execute_script('''
    
    var tag = document.querySelector('downloads-manager').shadowRoot;
    var intag = tag.querySelector('downloads-item').shadowRoot;
    var progress_tag = intag.getElementById('progress');
    var progress = null;
    if(progress_tag) {
        progress = progress_tag.value;
    }
    return progress;
    
    ''')
    return progress

driver = webdriver.Chrome()

# do some staff

driver.get('chrome://downloads/')

progress = get_download_progress()

print("Progress: ", progress)

Getting progresses of all downloads-items

Using method querySelector() you get the first element that matches, that is, you get the first download-item in the downloads list.

To get all download-items, you can use method querySelectorAll():

def get_download_progress_all():
    progress = driver.execute_script('''
    
    var tag = document.querySelector('downloads-manager').shadowRoot;
    var item_tags = tag.querySelectorAll('downloads-item');
    var item_tags_length = item_tags.length;
    var progress_lst = [];
    for(var i=0; i<item_tags_length; i++) {
        var intag = item_tags[i].shadowRoot;
        var progress_tag = intag.getElementById('progress');
        var progress = null;
        if(progress_tag) {
            var progress = progress_tag.value;
        }
        progress_lst.push(progress);
    }
    return progress_lst
    
    ''')
    return progress

progress = get_download_progress_all()

print("Progress: ", progress)

There you have it!

@adekmaulana
Copy link

not working :( in chrome 80 :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment