我想在https://www.osc.ca/en/securities-law/osc-bulletin?keyword=61-101&date[min]=&date[max]=&sort_bef_combine=field_start_date_DESC搜索關鍵字 ' 61-101'。這是我的代碼
service = Service(r"C:\Users\Lenovo\Desktop\chromedriver.exe")
driver = webdriver.Chrome(service=service)
driver.get('https://www.osc.ca/en/securities-law/osc-bulletin')
search = driver.find_element(By.XPATH, '//*[@id="edit-keyword"]')
search_word = '61-101'
search.send_keys(search_word)
search.send_keys(Keys.ENTER)
for i in range(1, 21):
sleep(2)
issue_path = '//*[@id="block-osc-glider-content"]/article/section[3]/div[2]/section[3]/div/div/div/div/div[2]/div/div[3]/div/div[2]/div[' str(i) ']/div/div[1]/div[2]/span[1]/a'
issue = driver.find_element(By.XPATH, issue_path)
issue.send_keys(Keys.ENTER)
driver.switch_to.window(driver.window_handles[1])
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="icon"]/iron-icon//svg')))
download = driver.find_element(By.XPATH, '//*[@id="icon"]/iron-icon//svg')
download.send_keys(Keys.ENTER)
driver.switch_to.window(driver.window_handles[0])
但是,這會產生 TimeoutException,我嘗試為下載按鈕提供不同的 XPATH,但它仍然找不到下載元素。我猜這個問題可能源于驅動程式無法切換到新標簽的事實。
uj5u.com熱心網友回復:
options = Options()
download_dir = os.getcwd()
prefs = {
"download.default_directory": download_dir,
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"plugins.always_open_pdf_externally": True
}
options.add_experimental_option("prefs", prefs)
service = Service(r"C:\Users\Lenovo\Desktop\chromedriver.exe")
driver = webdriver.Chrome(service=service,options=options)
wait = WebDriverWait(driver, 20)
driver.get('https://www.osc.ca/en/securities-law/osc-bulletin')
search = driver.find_element(By.XPATH, '//*[@id="edit-keyword"]')
search_word = '61-101'
search.send_keys(search_word)
search.send_keys(Keys.ENTER)
for i in range(1, 21):
sleep(2)
issue_path = '//*[@id="block-osc-glider-content"]/article/section[3]/div[2]/section[3]/div/div/div/div/div[2]/div/div[3]/div/div[2]/div[' str(i) ']/div/div[1]/div[2]/span[1]/a'
issue = driver.find_element(By.XPATH, issue_path)
issue.send_keys(Keys.ENTER)
wait.until(EC.number_of_windows_to_be(2))
driver.switch_to.window(driver.window_handles[1])
driver.close()
driver.switch_to.window(driver.window_handles[0])
要下載您需要在選項中使用 pref 自動下載的所有 pdf,我將其設定為檔案所在的當前目錄,但您可以將 download_dir 切換到您想要的任何檔案夾路徑。
我還建議等待手柄長度大于 1。
進口:
from selenium.webdriver.chrome.options import Options
import os
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/492876.html
