使用Python和Selenium在Internet Explorer 11中自动下载文件。

问题描述 投票:0回答:1

我试图通过使用Python和Selenium在多个Internet Explorer 11窗口同时下载一些Excels文件。当出现 "另存为 "弹出窗口时,问题就出现了,而点击保存按钮的唯一方法是发送键(alt + s)。但要做到这一点,焦点必须在浏览器窗口上,就像我之前说的,我需要在同一时间启动多个IE11窗口,做同样的事情。

像AutoIt,Robot或者仅仅是事件发送键这样的工具是无效的,因为这些工具使用的是操作系统,我想一定有一个解决方案,比如javascript,或者Python来处理每个窗口的浏览器,应该是有效的。

我很感激你的帮助,谢谢!

javascript python selenium download internet-explorer-11
1个回答
0
投票

据我所知,当你点击下载链接或按钮时,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路径和网站网址改为自己的。

更多关于使用wget的详细信息,你可以参考一下 本文.

© www.soinside.com 2019 - 2024. All rights reserved.