1

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()']"
8
  • 1
    Does it work manually ? Commented Jul 20, 2021 at 6:16
  • @Muhammed Ezzat Can you just try this XPath ((//*[normalize-space()='Download full list'])[3]) for the download button Commented Jul 20, 2021 at 7:25
  • @YaDavMaNish Can't find that XPath unfortunately. Commented Jul 20, 2021 at 7:30
  • @MuhammadEzzat try with incognito mode Commented Jul 20, 2021 at 7:38
  • @YaDavMaNish : why would you suggest that xpath, when we already have a good css selector ? What's the need to perform this in incognito mode ? Commented Jul 20, 2021 at 7:40

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

5 Comments

I tried using this line, still no downloaded file. If you successfully downloaded the file, could the problem come from another source? I'm really new to Selenium & still figuring things out. :D
Well I could download. What went wrong ? Can you remove driver.close and check again ?
I did so. There are no errors, it just finishes executing, prints the button text & nothing else.
Couldn't download too. I will try checking it on another machine.
Sorry for being late, I was on a vacation. Yes, it works indeed. I guess there is an issue that needs to be solved with my machine., perhaps the OS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.