簡體   English   中英

使用 Python 和 Selenium 在 Internet Explorer 11 中自動下載文件

[英]Download files automatically in Internet Explorer 11 with Python and Selenium

我正在嘗試使用 Python 和 Selenium 同時通過多個 Internet Explorer 11 windows 下載一些 Excels 文件。 當“另存為”彈出窗口出現並且單擊保存按鈕的唯一方法是發送密鑰(alt + s)時,問題就出現了。 但要做到這一點,焦點必須在瀏覽器 window 上,正如我之前所說,我需要同時啟動多個 IE11 windows 並做同樣的事情。

AutoIt、Robot 或僅事件發送密鑰等工具無效,因為這些工具使用操作系統,我認為必須有像 javascript 或 Python 之類的解決方案來處理每個應該工作的 window 瀏覽器。

感謝您的幫助,謝謝!

據我所知,當您單擊下載鏈接或按鈕時,WebDriver 無法訪問瀏覽器顯示的 IE 下載對話框。 但是,我們可以使用名為“ wget ”的單獨程序繞過這些對話框。

通過使用這個程序,我們首先可以得到超鏈接的href屬性值,然后,執行命令提示符命令從鏈接中下載文件。

示例代碼:

import time
import os
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

cap = DesiredCapabilities().INTERNETEXPLORER
cap['ignoreZoomSetting'] = True
driver = webdriver.Ie("D:\\Downloads\\webdriver\\IEDriverServer_x64_3.14.0\\IEDriverServer.exe",capabilities= cap)


driver.get("<website url>")

time.sleep(3)

btn = driver.find_element_by_id("btnDowloadReport")


hrefurl = btn.get_attribute("href")

os.system('cmd /c D:\\temp\\wget.exe -P D:\\temp --no-check-certificate ' + hrefurl)


print("*******************")

Html 資源:

<a id="btnDowloadReport" href="https://github.com//sakinala/AutomationTesting/raw/master/samplefile.pdf" >Download</a>

【注意】:請記住將webdriver路徑和網站url改成自己的。

有關使用 wget 的更多詳細信息,您可以參考這篇文章

在 Java 中嘗試過,文件已下載。

driver.findElement(By.xpath("//body")).sendKeys(Keys.chord(Keys.CONTROL, "j"));
Thread.sleep(2000);
Robot rb = new Robot();
rb.keyPress(KeyEvent.VK_ENTER);

說明:

  1. 首先點擊按鈕/鏈接下載文件。會看到這個彈出窗口在此處輸入圖像描述
  2. 單擊“Ctrl+j”這將打開“查看下載彈出窗口”。 然后點擊回車,因為焦點會在最近的文件上,所以會被下載

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM