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!

@Zik42
Copy link

Zik42 commented Aug 29, 2018

shanodRoot is null, why?

@MannarElkady
Copy link

shanodRoot is null, why?

No one replied to you yet?!

@CarlinWilliamson
Copy link

Works for me. Are you running the whole thing or just the function?

@ic0n
Copy link
Author

ic0n commented Aug 30, 2019

shanodRoot is null, why?

No one replied to you yet?!

Ah! The shadowRoot shouldn't be null.

But if the download item thingy isn't from the current session, and it won't have a progress bar, then my method will fail. I think I should add a checking to fix that.

@ChrisRahme
Copy link

ChrisRahme commented Dec 19, 2019

But if the download item thingy isn't from the current session, and it won't have a progress bar, then my method will fail. I think I should add a checking to fix that.

Did you find a fix? Also, what are the values that get returned? Booleans or ints/strings between 0 and 100?

Edit: I just used a try/except block. Turn out I got this error because the download finished too fast.

@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