使用Selenium + Firefox下载文件的问题

时间:2016-09-02 09:41:30

标签: python python-3.x selenium firefox

所以,我一直在尝试使用Firefox下载.csv文件和使用Python下载Selenium但直到现在都没有运气。

基本上我正在迭代选择框并对其进行过滤,对于每个过滤器,我想下载相应的.csv文件,然后继续下一个。

我似乎无法让这个适用于一个城市或所有城市。

我在SO上阅读了其他问题,但似乎没有任何问题适用于我的案例。

这是我目前的代码:

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()

使用此代码我遇到了

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?)

错误。

有什么想法吗?

0 个答案:

没有答案