* This method is tested on Chrome Version 61.0.3163.100, chromedriver Version 2.33.506106
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)
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)
not working :( in chrome 80 :(