Forums

Cannot download files by Selenium + Pyvirtualdisplay + Firefox

Hi there, I am trying to use selenium to download a .csv file but it doesn't work. I have searched around for a while. Any thoughts would be greatly appreciated! Here is my code:

from pyvirtualdisplay import Display
from selenium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By

with Display():
    # we can now start Firefox and it will run inside the virtual display

    profile = webdriver.FirefoxProfile()
    profile.set_preference('browser.download.folderList', 2) # custom location
    profile.set_preference('browser.download.manager.showWhenStarting', False)
    profile.set_preference('browser.download.dir', '/home/mmc777/dsp/SELENIUM')
    profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/csv')
    driver = webdriver.Firefox(profile)
    # put the rest of our selenium code in a try/finally
    # to make sure we always clean up at the end
    try:

        #driver.set_window_size(1120, 550)

        user_name = '******'
        password = '*******'
        driver.get("https://track.cruxsystems.com/login")
        #print(driver.page_source)
        driver.implicitly_wait(30)

        #driver.find_element_by_id('tab_ct').click()
        driver.find_elements_by_xpath("//input[@type = 'email']")[0].send_keys(user_name)
        driver.find_elements_by_xpath("//input[@type = 'password']")[0].send_keys(password)
        driver.find_elements_by_xpath("//input[@type = 'submit']")[0].click()
        #driver.explicitly_wait(30)
        #info = driver.find_element_by_id("ref_info_premain")

        driver.implicitly_wait(30)
        driver.find_elements_by_xpath("//button[@uib-tooltip='Containers']")[0].click()
        time.sleep(5)
        driver.find_elements_by_xpath("//button[@uib-tooltip='Select all']")[0].click()
        WebDriverWait(driver, 50).until(
            expected_conditions.element_to_be_clickable(
                (By.XPATH, '//button[@uib-tooltip="Download"]')))
        driver.find_elements_by_xpath("//button[@uib-tooltip='Download']")[0].click()
        #result = driver.find_elements_by_xpath("//div[@class='flex-33']")[0]
        #print(result.text)

    finally:
        driver.quit()
        print('finished')

When you say it doesn't work- is there for example an error stacktrace that you could show to help debug?

@conrad No, it does not give me any error message and does not download the file either.

Is this the kind of download button where the browser would normally pop up a dialog asking you where the downloaded file should be saved, or perhaps make it appear in a "downloaded files" bar at the bottom of the window? If so, check out this Stack Overflow answer for some code that will allow you to do that.

can you show example to implement the code inside this pythonanywhere? Thank you

We do not have any ready examples for that.