I have been trying to build a script to automate the download of a statistics file.
The site is JS heavy and there is no link readily available. I have been able to build a script that executes the necessary steps, but I am struggling to actually successfully download the file. Any tips on how I can ensure that the file is actually downloaded before closing down the browser?
from seleniumbase import Driver
downloads_folder = "\\downloads"
driver = Driver(browser="chrome")
driver.execute_cdp_cmd("Page.setDownloadBehavior", {"behavior": "allow", "downloadPath": downloads_folder})
username = "xxxxx"
password = "xxxxx"
try:
driver.open("https://new.stamdata.com/app/statistics")
driver.click('button:contains("Account")')
driver.click("div#app div div:nth-of-type(2) span p")
driver.type('input[name="username"]', username)
driver.type('input[name="password"]', password)
driver.click('button:contains("sign in")')
driver.assert_element('button:contains("Excel export (max 50 000 rows)")')
driver.click('button:contains("Excel export (max 50 000 rows)")')
# add wait for download
driver.click('button:contains("menu")')
driver.click("div#app div div:nth-of-type(3) span p")
driver.click('button:contains("log out")')
finally:
driver.quit()
I have tried various waits and timeouts, but nothing seems to work.
The
SB()format of SeleniumBase has more methods than theDriver()format. WithSB(), you can callsb.assert_downloaded_file(filename)to see the the file was downloaded successfully.Since your full script is behind a login, I'll demonstrate with another example that downloads chromedriver notes and verifies the content: