微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用selenium-python从弹出对话框中自动下载文件

我试图使用selenium-python从弹出对话框中自动下载文件.

firefox弹出窗口看起来像这样

我想模拟点击“确定”

我发现这个答案How do I trap a popup in Selenium 2 python将我发送到了文档https://selenium-python.readthedocs.org/en/latest/navigating.html?highlight=popup#popup-dialogs

我试过这个

    alert = driver.switch_to_alert()
    #alert.send_keys(Keys.RETURN) #No alert is present

和这个

    alert = driver.switch_to_alert()
    alert.accept()  #no alert is present

如果我运行pprint.pprint(driver.window_handles),它只打印一个GUID – 显示只有一个窗口存在.

因此,如果没有警报且只有一个窗口 – 我该如何下载这些文件

解决方法:

在python中,但这也适用于Java,因为firefox首选项是一个javascript:

profile.set_preference("browser.download.panel.shown", False)
profile.set_preference("browser.helperApps.neverAsk.openFile","text/csv,application/vnd.ms-excel")
profile.set_preference("browser.helperApps.neverAsk.savetodisk", "text/csv,application/vnd.ms-excel")
profile.set_preference("browser.download.folderList", 2);
profile.set_preference("browser.download.dir", "c:\\firefox_downloads\\")
browser = webdriver.WebDriver(firefox_profile=profile)

这适用于CSV文件,可以根据要下载的任何文件类型进行修改.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐