Search code examples
pythonselenium-webdrivergoogle-sheetsweb-testing

How to download a spreadsheet from Google Sheets as a CSV using selenium? In particular, how to click the final CSV suboption?


screenshot of google sheets menus and submenus

I am trying to download a google spreadsheet using selenium. The idea is to click the File menubar, then the Download submenu, and finally the Comma-seperated values (.csv) suboption.

I've configured webdriver to download files (set up the default download directory, disabled prompting, etc.) and to avoid getting blocked by google (I'm using undetected-chromedriver with proxies) and I've managed to click the File menubar and the Download submenu but I haven't been able to figure out how to click the CSV suboption.

Here's what I have till now:

driver = get_driver() # a function that sets up a chromedriver instance with a bunch of options
driver.get(r"https://docs.google.com/spreadsheets/d/10X0-CCSv7FenZP1YaKlDSE-PD4LSZAgpzT5Rs9F8hvA/")

# click the file menu (this works)

file_element = WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.ID, "docs-file-menu")))
ActionChains(driver).move_to_element(file_element).click().perform()

# click the download submenu (this works too)

download_element = WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.ID, ":8i")))
ActionChains(driver).move_to_element(download_element).click().perform()

# click the csv element (I'm not sure how to do this)

# ...?

Finding the CSS selector for the File menu bar was straightforward. I had to use the 'Pause in debugger' option to inspect the Downloads submenu. But I haven't been able to find the CSS selector for the file type suboption because it's not possible to inspect.

Does anybody have any idea how I can click this?enter image description here


Solution

  • In general, you don't need to use dropdown to download csv file. You can just open your file with path {link}/export?format=csv

    driver = webdriver.Chrome()
    driver.get(r"https://docs.google.com/spreadsheets/d/10X0-CCSv7FenZP1YaKlDSE-PD4LSZAgpzT5Rs9F8hvA/export?format=csv")
    

    Selenium solution:

    You can locate dropdown items by stopping execution in debug mode with opened Dev Tools and searching for text with dropdown item.

    Second option - you can define selector that represents text exact or partial match of your item.

    I wrote combined approach: download item has selector [id=':8i'] and you can locate .csv submenu option by xPath expression //*[contains(.,'csv')]

    driver = webdriver.Chrome()
    driver.get(r"https://docs.google.com/spreadsheets/d/10X0-CCSv7FenZP1YaKlDSE-PD4LSZAgpzT5Rs9F8hvA/")
    action_chains = ActionChains(driver)
    wait = WebDriverWait(driver, 10)
    file_element = wait.until(EC.element_to_be_clickable((By.ID, "docs-file-menu")))
    action_chains.move_to_element(file_element).click().perform()
    download = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "[id=':8i']")))
    action_chains.move_to_element(download).perform()
    wait.until(EC.visibility_of_element_located((By.XPATH, "//*[contains(.,'csv')]"))).click()
    time.sleep(5)