pythonseleniumiframeautomationtableau-api

How can I programmatically download Tableau csv file from website with Python?


I am attempting to download the data from the following site programmatically with Python: https://health.wyo.gov/publichealth/infectious-disease-epidemiology-unit/disease/novel-coronavirus/covid-19-testing-data/

At the bottom right of the Tableau view there are 3 buttons: Share, Download, and Full Screen. After clicking Download, you are then taken to another pop up. I am then wanting to select Crosstab that then takes you to another pop up where I want to select Positivity and finally, Download which provides a csv.

I have essentially managed to navigate through some of the iframes but am a little lost on where to click since the buttons do not include id/link.

Below is some code from one approach:

from selenium import webdriver

url = 'https://public.tableau.com/profile/melissa.taylor#!/vizhome/WyomingCOVID-19TestingDataDashboard/Dashboard1'

driver = webdriver.Chrome()
driver.get(url)
#elem = driver.switch_to.frame(driver.find_element_by_xpath('//iframe[contains(text(), "googletagmanager"]'))
try:
    time.sleep(4)
    iframe = driver.find_elements_by_tag_name('iframe')[0]
    driver.switch_to.default_content()

    driver.switch_to.frame(iframe)
    driver.find_elements_by_tag_name('iframe')
    #driver.find_element_by_id('download-ToolbarButton').click()

    print(driver.page_source)
finally:
    driver.quit()

Below is the HTML that displays the 3 buttons from the Tableau page.

<div class="tab-nonVizItems tab-fill-right hideLabels"><div class="tabToolbarButton tab-widget undo disabled" role="button" data-tb-test-id="undo-ToolbarButton" id="undo-ToolbarButton" aria-disabled="true" tabindex="-1" style="user-select: none; -webkit-tap-highlight-color: transparent;" title="Undo"><span class="tabToolbarButtonImg tab-icon-undo"></span><span class="tabToolbarButtonText">Undo</span></div><div class="tabToolbarButton tab-widget redo disabled" role="button" data-tb-test-id="redo-ToolbarButton" id="redo-ToolbarButton" aria-disabled="true" tabindex="-1" style="user-select: none; -webkit-tap-highlight-color: transparent;" title="Redo"><span class="tabToolbarButtonImg tab-icon-redo"></span><span class="tabToolbarButtonText">Redo</span></div><div class="tabToolbarButton tab-widget revert disabled" role="button" data-tb-test-id="revert-ToolbarButton" id="revert-ToolbarButton" aria-disabled="true" tabindex="-1" style="user-select: none; -webkit-tap-highlight-color: transparent;" title="Reset"><span class="tabToolbarButtonImg tab-icon-revert"></span><span class="tabToolbarButtonText">Reset</span></div><div class="tabToolbarButton tab-widget share" role="button" data-tb-test-id="share-ToolbarButton" id="share-ToolbarButton" tabindex="-1" style="user-select: none; -webkit-tap-highlight-color: transparent;" title="Share"><span class="tabToolbarButtonImg tab-icon-share"></span><span class="tabToolbarButtonText">Share</span></div><div class="tabToolbarButton tab-widget download" role="button" data-tb-test-id="download-ToolbarButton" id="download-ToolbarButton" tabindex="-1" style="user-select: none; -webkit-tap-highlight-color: transparent;" title="Download"><span class="tabToolbarButtonImg tab-icon-download"></span><span class="tabToolbarButtonText">Download</span></div><div class="tabToolbarButton tab-widget enterFullscreen" role="button" data-tb-test-id="toggle-fullscreen-ToolbarButton" id="toggle-fullscreen-ToolbarButton" tabindex="-1" style="user-select: none; -webkit-tap-highlight-color: transparent;" title="Full Screen"><span class="tabToolbarButtonImg tab-icon-enterFullscreen"></span><span class="tabToolbarButtonText">Full Screen</span></div></div>
            <div></div></div><div class="tab-ReactView tab-toolbar-dialoghost"></div></div></div>

Thanks for your help!!


Solution

  • Try the below code:

    driver.get('https://public.tableau.com/profile/melissa.taylor#!/vizhome/WyomingCOVID-19TestingDataDashboard/Dashboard1')
    wait = WebDriverWait(driver, 20)
    wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[title='Data Visualization']")))
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".tab-icon-download"))).click()
    wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Crosstab']"))).click()
    wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='positivity']"))).click()
    wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Download']"))).click()
    

    Following import:

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC