Skip to content

Instantly share code, notes, and snippets.

@nim4n136
Last active February 25, 2020 08:15
Show Gist options
  • Save nim4n136/263fe4fb9209b2ea7d3a89fde6d72c90 to your computer and use it in GitHub Desktop.
Save nim4n136/263fe4fb9209b2ea7d3a89fde6d72c90 to your computer and use it in GitHub Desktop.
Selenium checking download
# method to get the downloaded file name
def getDownLoadedFileName(waitTime):
driver.execute_script("window.open()")
# switch to new tab
driver.switch_to.window(driver.window_handles[-1])
# navigate to chrome downloads
driver.get('chrome://downloads')
# define the endTime
endTime = time.time()+waitTime
while True:
try:
# get downloaded percentage
downloadPercentage = driver.execute_script(
"return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('#progress').value")
# check if downloadPercentage is 100 (otherwise the script will keep waiting)
if downloadPercentage == 100:
# return the file name once the download is completed
return driver.execute_script("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('div#content #file-link').text")
except:
pass
time.sleep(1)
if time.time() > endTime:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment