1

So, i have been trying to download .csv files using Firefox and Selenium with Python but no luck until now.

Basically i am iterating through a select box and filtering it, for each filter i want to download the corresponding .csv file and then continue with the next one.

I can't seem to get this working for one city or all of them.

I read other questions on SO but nothing seems to work for my case.

Here is my current code:

def download_csv(self, path=management/):
    profile = webdriver.FirefoxProfile()
    profile.set_preference("browser.download.folderList", 2)
    profile.set_preference("browser.download.manager.showWhenStarting",
                           False)
    profile.set_preference("browser.download.dir", '/tmp')
    profile.set_preference("browser.helperApps.neverAsk.saveToDisk",
                           "text/csv")

    display = Display(visible=0, size=(1024, 768))
    display.start()
    driver = webdriver.Firefox(profile)
    driver.maximize_window()

    url = 'https://rnt.turismodeportugal.pt/RNAL/ConsultaRegisto.aspx' \
          '?Origem=CP&FiltroVisivel=True'
    driver.get(url)

    select_options = []

    select = driver.find_element_by_css_selector('#wt32')
    for option in select.find_elements_by_tag_name('option'):
        if option.text != 'Todos' and option.text != '':
            select_options.append(option.text)

    print (len(select_options))

    for option in select_options:
        driver.get(url)
        select = Select(driver.find_element_by_id('wt32'))
        print (option)
        select.select_by_visible_text(option)
        search = driver.find_element_by_css_selector('#wt10')
        search.click()
        print(driver.find_element_by_css_selector('.Counter_Message').text)

        download = driver.find_element_by_css_selector('#wt84')
        download.click()

        print ('***********')

    driver.quit()

With this code i run into a

selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: A exportação da informação para excel poderá demorar alguns minutos. Confirma Exportar dados?
Message: Unexpected modal dialog (text: A exportação da informação para excel poderá demorar alguns minutos. Confirma Exportar dados?)

error.

Any ideas?

psychok7
  • 5,373
  • 9
  • 63
  • 101
  • at what point in the code are you receiving this error? – Breaks Software Sep 02 '16 at 11:39
  • after the first iteration. if i don't iterate i dont get the error but the file does not download stilll – psychok7 Sep 02 '16 at 11:47
  • (I'm assuming that you can't watch this test run, so that you can see what the alert is) add a try/catch block in your loop to catch this exception, then in your catch block, get the text of the alert and throw it into your log. This will at least tell you what's happening. – Breaks Software Sep 02 '16 at 16:44
  • @BreaksSoftware i have updated my code, i can now select each city but the download throws me the error i posted above – psychok7 Sep 02 '16 at 16:46
  • Can you translate the message? At a minimum, it looks like it's asking you to confirm something before you do the download. Do you get then when you download manually? and do you confirm when you download manually? If so, then you can use the Alert class to accept: Alert alert = driver.switchTo().alert(); alert.accept(); – Breaks Software Sep 06 '16 at 12:57
  • Could this be the root cause? A bypassed call to fxdriver.modals.clearFlag_ ... cf. https://stackoverflow.com/questions/44568402/how-do-i-manually-mouse-dismiss-a-javascript-alert-and-get-back-the-the-body-o/44592827#44592827 – NevilleDNZ Jun 16 '17 at 23:56

0 Answers0