Issue with downloading files using seleniumbase

26 views Asked by At

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.

1

There are 1 answers

0
Michael Mintz On

The SB() format of SeleniumBase has more methods than the Driver() format. With SB(), you can call sb.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:

from seleniumbase import SB

with SB(test=True) as sb:
    sb.open("https://chromedriver.chromium.org/downloads")
    notes_file = "notes.txt"
    notes_link = (
        "https://chromedriver.storage.googleapis.com"
        "/101.0.4951.41/%s" % notes_file
    )
    sb.download_file(notes_link)
    sb.assert_downloaded_file(notes_file)

    notes_path = sb.get_path_of_downloaded_file(notes_file)
    with open(notes_path, "r") as f:
        notes_data = f.read()
    sb.assert_true(len(notes_data) > 100)  # Verify file not empty
    text = "Switching to nested frame fails with chrome/chromedriver 100"
    sb.assert_true(text in notes_data)  # Verify file has expected data