我已经尝试了 this 中的所有解决方案非常相似的帖子,但不幸的是,虽然我没有收到任何有用的错误,但我的文件夹中也没有收到任何 pdf 文件。
要更改配置以便 selenium headless 工作并下载到我想要的目录,我遵循了这个 post和 this .
然而我什么也没看到。此外,交互执行与运行脚本时的行为也不同。以交互方式执行时,我没有看到任何错误,但也没有任何 react 。运行脚本时,我收到一个不太有用的错误:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, f"a[href*={css_selector}']"))).click()
File "C----\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
有问题的网站是 here .我试图使工作的代码是-
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.headless = True
uri = "http://affidavitarchive.nic.in/CANDIDATEAFFIDAVIT.aspx?YEARID=March-2017+(+GEN+)&AC_No=1&st_code=S24&constType=AC"
driver = webdriver.Firefox(options=options, executable_path=r'C:\\Users\\xxx\\geckodriver.exe')
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2) # custom location
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', r'C:\\Users\\xxx\\Downloads')
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/pdf')
# Function that reads the table in the webpage and extracts the links for the pdfs
def get_links_from_table(uri):
html = requests.get(uri)
soup = BeautifulSoup(html.content, 'lxml')
table = soup.find_all('table')[-1]
candidate_affidavit_links = []
for link in table.find_all('a'):
candidate_affidavit_links.append(link.get('href'))
return candidate_affidavit_links
candidate_affidavit_links_list = get_links_from_table(uri)
driver.get(uri)
# iterate over the javascript links and try to download the pdf files
for js_link in candidate_affidavit_links_list:
css_selector = js_link.split("'")[1]
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, f"a[href*={css_selector}']"))).click()
driver.execute_script(js_link)
最佳答案
如果这一切都可以用 Selenium 完成,我会试试这个:
driver.get(uri)
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "(//table//a)[last()]")))
time.sleep(1)
candidate_affidavit_links = driver.find_elements_by_xpath("(//table//a)[last()]")
for link in candidate_affidavit_links:
link.click()
time.sleep(1)
打开页面,等待至少表中的第一个链接可见,再添加一些等待直到所有表肯定加载,获取所有a
(链接)元素到列表,遍历该列表,单击这些元素并在每次单击后延迟以完成下载。可能您需要在单击每个链接以完成下载文件后放置更长的延迟,然后才能开始下一次下载。
UPD
要禁用要求保存文件等的弹出窗口,请尝试以下操作:
而不是仅仅
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/pdf')
把这个:profile.set_preference('browser.helperApps.neverAsk.saveToDisk", "application/csv,application/excel,application/vnd.ms-excel,application/vnd.msexcel,text/anytext,text/comma-separated-values,text/csv,text/plain,text/x-csv,application/x-csv,text/x-comma-separated-values,text/tab-separated-values,data:text/csv')
profile.set_preference('browser.helperApps.neverAsk.saveToDisk", "application/xml,text/plain,text/xml,image/jpeg,application/octet-stream,data:text/csv')
profile.set_preference('browser.download.manager.showWhenStarting',false)
profile.set_preference('browser.helperApps.neverAsk.openFile","application/csv,application/excel,application/vnd.ms-excel,application/vnd.msexcel,text/anytext,text/comma-separated-values,text/csv,text/plain,text/x-csv,application/x-csv,text/x-comma-separated-values,text/tab-separated-values,data:text/csv')
profile.set_preference('browser.helperApps.neverAsk.openFile","application/xml,text/plain,text/xml,image/jpeg,application/octet-stream,data:text/csv')
profile.set_preference('browser.helperApps.alwaysAsk.force', false)
profile.set_preference('browser.download.useDownloadDir', true)
profile.set_preference('dom.file.createInChild', true)
不确定您是否需要所有这些,但我拥有所有这些并且对我有用
关于javascript - 如何在网页中执行 "javascript:__doPostBack"以使用 selenium 下载 pdf 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68151422/