我尝试使用 python selenium 库从网站下载 excel 文件。这是网址:https://www.worldbank.org/en/projects-operations/procurement/debarred-firms.
我尝试了下面的 javascript 执行器方法,但仍然没有运气。请在这里帮助我,我是自动化的新手
这是我的代码。
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)
driver.maximize_window()
# 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.element_to_be_clickable((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()
# WebElement webElement = webDriverWait.until(ExpectedConditions.elementToBeClickable(By.id(tabId)));
# JavascriptExecutor executor = (JavascriptExecutor)driver;
# executor.executeScript("arguments[0].click();", download_anchor);
driver.execute_script("arguments[0].click();",download_anchor)
except Exception as e:
print("Error: " + str(e))
finally:
driver.close()