Unable to download a file using Selenium Python

Question:

I am trying to download a file using the Selenium library from the source page = "https://ec.europa.eu/info/law/better-regulation/have-your-say/initiatives/12527-Artificial-intelligence-ethical-and-legal-requirements/F2665623_en"
you can see the file at the bottom of website

Following is the code I try to run


from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.chrome.options import Options       

driver.get('https://ec.europa.eu/info/law/better-regulation/have-your-say/initiatives/12527-Artificial-intelligence-ethical-and-legal-requirements/F2665640_en')
downloadfile = driver.find_element(By.CLASS_NAME, 'ecl-file__download')
time.sleep(4)
downloadfile.click();

but getting error

ElementClickInterceptedException: Message: element click intercepted: Element <a ecllink="" variant="standalone" class="ecl-file__download ecl-link ecl-link--standalone ecl-link--icon ecl-link--icon-after" href="" download="">...</a> is not clickable at point (237, 571). Other element would receive the click: <p class="wt-paragraph">...</p> (Session info: headless chrome=104.0.5112.101)

Asked By: Mubeen Iftikhar

||

Answers:

Instead of using

downloadfile.click();

You might need to use execute_script function for a, try following

driver.execute_script("arguments[0].click();", download_file)

The following is working code

from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

chrome_driver = 'path-to-drivechromedriver.exe'
driver = webdriver.Chrome(executable_path=chrome_driver)
driver.get('https://ec.europa.eu/info/law/better-regulation/have-your-say/initiatives/12527-Artificial-intelligence-ethical-and-legal-requirements/F2665640_en')
download_file = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.CLASS_NAME, "ecl-file__download")))
driver.execute_script("arguments[0].click();", download_file)
Answered By: Abdullah Mughal