Chromium headless: how to driver.quit() only after the downloads are finished?

61 views
Skip to first unread message

Andrea Borruso

unread,
Oct 7, 2023, 5:31:30 AM10/7/23
to Selenium Users
Hi,
I have the below sample code that clicks on all the download buttons on a page and downloads xml files.

How to run driver.quit() only after the downloads are finished?

If I add it at the end, it immediately closes the download and does not download all the files.

Thank you



import os
import urllib.parse
import requests
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(options=options)

driver.get("https://www.rna.gov.it/RegistroNazionaleTrasparenza/faces/pages/OpenDataMisura.jspx")

driver.implicitly_wait(10)

links = driver.find_elements(By.XPATH, "(//a[span[contains(text(),'Download') and @class='xfv']])")

for link in links:
    link. Click()

Adrian

unread,
Oct 8, 2023, 6:00:49 PM10/8/23
to Selenium Users
Hi,
You need to verify that the files have downloaded before the test finishes.

So at the start of the test, delete all the files in the download directory.  This will ensure that any files in the download directory belong to this test.
At the end of the test after the for loop, count the number of files in the download directory.
Keep waiting until the number of files in the download directory match the number of download links.  Make sure to ignore incomplete files, like those with the extension crdownload.

Reply all
Reply to author
Forward
0 new messages