I’ve been trying to click a button that downloads a CSV file from “https://mol.org/regions/?regiontype=countries”. I’m sure that I’ve selected the button, as I can print the text written on it, but whenever I try to .click()
it, it doesn’t download the file. Are there any additional steps needed to operate the function bound to the button? Thank you in advance.
PS : The button works manually.
Here is the driver code I used :
with webdriver as driver: driver.maximize_window() driver.implicitly_wait(30) driver.get(url) driver.maximize_window() wait = WebDriverWait(driver, 10) wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, search_bar_CSS_Selector))).send_keys(search_query+Keys.RETURN) driver.find_element_by_css_selector(download_button_CSS_Selector).click() print(driver.find_element_by_css_selector(download_button_CSS_Selector).text) driver.close()
You can see that I actually print the button text & can access it, but the .click()
is not working as expected.
Variables :
search_query = 'Egypt' search_bar_CSS_Selector = "input[placeholder='Filter Political boundaries']" download_button_CSS_Selector = "button[ng-click ='initiateDownload()']"
Advertisement
Answer
Your css selector
looks perfect, but I think it's a page loading issue
. So I tried that with an explicit wait command
(check below) and it seems working fine.
Sample code :
so instead of this :
driver.find_element_by_css_selector(download_button_CSS_Selector).click()
use this :
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[ng-click ='initiateDownload()']"))).click()
Update 1 :
driver.get("https://mol.org/regions/?regiontype=countries") driver.maximize_window() wait = WebDriverWait(driver, 10) wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[placeholder='Filter Political boundaries']"))).send_keys('Egypt'+Keys.RETURN) wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[ng-click ='initiateDownload()']"))).click()