如何使用 python selenium 从没有链接的网站下载文件?

问题描述 投票:0回答:0

朋友们,我正在尝试从这个世界银行组织网站下载 excel 文件 - https://www.worldbank.org/en/projects-operations/procurement/debarred-firms

我正在尝试使用 selenium 库通过 python 脚本自动执行任务来下载它。

我尝试过使用 XPath、Class 和 CSS 选择器方法……但没有找到解决方案。请看下面我试过的代码,并告诉我你的知识朋友。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
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.get('https://www.worldbank.org/en/projects-operations/procurement/debarred-firms');
    #downloadcsv= driver.find_element(By.XPATH, '//*[@id="k-debarred-firms"]/div[1]/a');
    #gotit= driver.find_element(By.CLASS_NAME, "dialog_form_actions");
    #gotit.click();
    #Click on Download Button
    #driver.find_element(By.XPATH,'//*[@id="k-debarred-firms"]/div[1]/a').click()
    #time.sleep(50)

    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()

    driver.close()
    print("file downloaded")

except:
    print("Invalid URL")`
python selenium-webdriver web-scraping browser-automation
© www.soinside.com 2019 - 2024. All rights reserved.