Search code examples
pythonselenium-webdriver

Click download button within accordion using selenium


I'm trying to get the data from this webpage: https://covid.cdc.gov/covid-data-tracker/#trends_weeklydeaths_select_00

It is my first time using selenium, and I can't find the button to expand the first accordion (while I found the one to expand the second one).

For now, I have:

driver = webdriver.Chrome()
driver.get('https://covid.cdc.gov/covid-data-tracker/#trends_weeklydeaths_select_00')
button = driver.find_element(By.ID,"WHAT-IS-THE-ID")
button.click()

Solution

  • For some reason the selenium is not able to interact with these web elements, when you cannot use selenium's click() or ActionChains to interact with the element, you can use JavaScript.

    Refer the working code below:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    driver = webdriver.Chrome()
    driver.get("https://covid.cdc.gov/covid-data-tracker/#trends_weeklydeaths_select_00")
    driver.maximize_window()
    # Below line creates WebDriverWait object which waits for 10s based on condition
    wait = WebDriverWait(driver, 10)
    # Below 2 lines will click on first accordion element using JavaScript
    accordion_btn = wait.until(EC.visibility_of_element_located((By.ID, "us-trends-table-toggle")))
    driver.execute_script("arguments[0].click();", accordion_btn)
    # Below 2 lines will click on download button using JavaScript
    download_btn = wait.until(EC.visibility_of_element_located((By.ID, "btnUSTrendsTableExport")))
    driver.execute_script("arguments[0].click();", download_btn)