使用 Selenium 单击文件 link,将 html 存储到变量中并下载带有特定文本的特定文件名

Use Selenium to click on a file link, store the html into a variable and download a specific filename with certainy text

需要协助完成其余任务:

  1. 单击并打开“SUMMARY”下的 link
  2. 将新的 html link 复制到变量以备将来使用。
  3. 将 pdf 下载到文件夹中

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Firefox(executable_path = "C:\Program Files\Mozilla Firefox\geckodriver.exe")
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://www1.hkexnews.hk/search/titlesearch.xhtml")
wait = WebDriverWait(driver, 20)
StockList = ['02192']
wait.until(EC.element_to_be_clickable((By.ID, "searchStockCode"))).send_keys(StockList[0])


wait.until(EC.element_to_be_clickable((By.ID, "searchTitle"))).send_keys( "Announcement of Offer Price and Allotment Results" )


ele = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@class='slimScrollDiv']/descendant::tbody/tr[1]")))
ActionChains(driver).move_to_element(ele).click().perform()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[class*='applyFilters']"))).click()

driver.find_element_by_link_text("Announcement of Offer Price and Allotment Results").click()

您可以使用以下 xpath :

a[rel='noopener noreferrer']

点击link。

new_link_element = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[rel='noopener noreferrer']")))
var_new_link = new_link_element.get_attribute('href')

如果你想点击它,你可以这样做:

new_link_element.click()

更新:

点击 Announcement of Offer Price and Allotment Results 后,将打开一个新标签页,

所以接下来你必须像这样切换驱动程序焦点:

driver.switch_to.window(driver.window_handles[1])
wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "SUMMARY"))).click()