How can I click on the select bar made of js in the selenium?

Viewed 94

I'm crawling Google spreadsheets using Python and Selenium. I want to click Download in the selection bar that comes out when I enter the sheet and click the file button, but the problem is that the selection bar is composed of js. In this case, how can I click the button? I'll attach a picture.

enter image description here

I want you to click the button in the red circle and then click the download button that comes out.

1 Answers

In order to get Download button, use

driver.find_element_by_css_selector('span[aria-label="Download d"]')

To download as excel use:

driver.find_element_by_css_selector('.goog-menuitem-content>span[aria-`label="Microsoft Excel (.xlsx) x"]')

For locating your elements use:

  1. Open devtools
  2. Select Elements tab
  3. Open your dropdown
  4. Press F8 to pause execution of JS.

Try the following script:

from selenium.webdriver import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By

wait_for_element_to_be_clickable(select_type=By.ID,
                                         element="docs-menubar")
files_menu = driver.find_element_by_id("docs-menubar")
files_menu.click()

wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'span[aria-label="Download d"]')))
download_dropdown = driver.find_element_by_css_selector('span[aria-label="Download d"]')
excel_locator = driver.find_element_by_css_selector('.goog-menuitem-content>span[aria-`label="Microsoft Excel (.xlsx) x"]')

actions = ActionChains(driver)
actions.move_to_element(download_dropdown)
actions.move_to_element(excel_locator)
actions.click().perform()

Play with waits. Selectors are already good.

Related