通過使用CHrome瀏覽器來保存PDF并沒有引起任何問題(我使用的是這些選項):
options.add_experimental_option('prefs'/span>,{
'credentials_enable_service': False,
'plugins':{
'always_open_pdf_externally': True。
},
'profile': {
'password_manager_enabled': False,
},
'download': {
'tip_for_download': False,
'directory_upgrade': True,
'default_directory': ''。
}
})
BUT .... 如何通過使用webdriver.Ie()Internet Explorer驅動與Python Selenium來保存PDF?
uj5u.com熱心網友回復:
你不能用Selenium來處理IE中的下載提示,因為那是一個作業系統級別的提示。Selenium WebDriver沒有能力自動處理作業系統級的提示視窗。你需要使用一些第三方工具來幫助你使用 Selenium 在 IE 中下載檔案。
這里我使用Wget來繞過下載提示并在IE中下載檔案。你可以參考這篇文章,了解如何使用Wget。
。關于在 Selenium 中的 IE 中使用無頭模式,您還可以使用一個名為 headless_ie_selenium 的第三方工具。你可以下載這個工具并使用 headless_ie_selenium.exe 代替 IEDriverServer.exe 來自動化 IE。
下載pdf檔案的示例代碼如下,請注意將代碼中的路徑改成你自己的:
from selenium import webdriver
import time
import os
url = "https://file-examples.com/index.php/sample-documents-download/sample-pdf-download/"/span>
driver = webdriver.Ie('D:headless-selenium-for-win-v1-4headless_ie_selenium.exe' )
driver.get(url)
time.sleep(3)
link = driver.find_elements_by_class_name("download-button") [0]
hrefurl = link.get_attribute("href")
os.system('cmd /c C:Wgetwget.exe -P D:Download --no-check-certificate ' hrefurl)
print("*******************")
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/320222.html
標籤:
