How to download a file from website without link using python selenium?
Sorry for the delay! I found your elements! But the the things are not so simple as a ‘click’. The anchor/link is not “clickable”, probably because some nasty Javascript is doing nasty things and you’ll have to find out what is going on.
Anyways, I hope this code below helps somehow:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("--log-level=OFF")
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
try:
driver.execute("get", {'url': 'https://www.worldbank.org/en/projects-operations/procurement/debarred-firms'})
# WebDriverWait(driver, 200).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'title="Excel"'))).click()
wait = WebDriverWait(driver, 500)
# Waiting for the searching input text
text = wait.until(EC.presence_of_element_located((By.ID, "category")))
print("INPUT TEXT FOUND: ", str(text.get_attribute('outerHTML')))
# waiting for the download anchor/link
download_anchor = wait.until(EC.presence_of_element_located((By.XPATH, "/html/body/div[3]/div[2]/div/div/div/div/div/div[1]/div[2]/div/div/div/div[5]/div[2]/div/div/div[3]/div[1]/a")))
print("DOWNLOAD_ANCHOR: ", str(download_anchor.get_attribute('outerHTML')))
download_anchor.click()
except Exception as e:
print("Error: " + str(e))
finally:
driver.close()
Check out the error message! Probably there some new clues there.
Read more here: Source link
