在python中通过Selenium Webdriver下载文件 [英] Downloading file through Selenium Webdriver in python

查看:484
本文介绍了在python中通过Selenium Webdriver下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,以通过python中的Selenium Webdriver自动进行Web交互.当我通过脚本单击下载"按钮时,我陷入了最后一步,屏幕上弹出了一个窗口,并选择了默认选项打开方式".我希望程序首先单击选项保存文件",然后单击确定".我使用以下代码来设置Firefox配置文件

I am writing a program to automate web interaction through selenium webdriver in python. I got stuck in last step when I click on the "download" button through script, a window pop-up occours on the screen,with default option "Open with" selected. I want my program to first click on the option "save file" and then click on "OK". I have used following piece of code to set up Firefox profile

profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', os.getcwd())
profile.set_preference('browser.helperApps.neverAsk.saveToDisk',"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")

我的观察之一是,当窗口弹出时是这样的

One of my observation is that when the window popup is like this

带有选项从现在开始自动对类似文件执行此操作"是可以单击的(通过复选框),那么上面的代码可以完美地工作,但是当相同的选项不可单击时(如下图所示),则上面的代码设置配置文件的代码失败. 在这种情况下有人可以帮助我吗?

with option "Do this automatically for files like this from now on" is clickable (via checkbox) then the above piece of code works perfect, but when the same option is not clickable (as shown in the image below) then above code for setting profile fails. Can anyone help me in this situation?

推荐答案

在使用新的FirefoxProfile时,请使用set_preference方法以这种方式配置配置文件,以便单击Save并且在下载过程中不会被中断.您可以按照以下方式设置配置:

While you work with a new FirefoxProfile, use the set_preference method to configure the profile in such a way so clicks on Save and Ok and it doesn't gets interrupted in the downloading process. You can set the configuration as follows:

profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.dir",os.getcwd());
profile.set_preference("browser.download.folderList",2);
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/csv,application/excel,application/vnd.msexcel,application/vnd.ms-excel,text/anytext,text/comma-separated-values,text/csv,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/octet-stream");
profile.set_preference("browser.download.manager.showWhenStarting",False);
profile.set_preference("browser.helperApps.neverAsk.openFile","application/csv,application/excel,application/vnd.msexcel,application/vnd.ms-excel,text/anytext,text/comma-separated-values,text/csv,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/octet-stream");
profile.set_preference("browser.helperApps.alwaysAsk.force", False);
profile.set_preference("browser.download.manager.useWindow", False);
profile.set_preference("browser.download.manager.focusWhenStarting", False);
profile.set_preference("browser.download.manager.alertOnEXEOpen", False);
profile.set_preference("browser.download.manager.showAlertOnComplete", False);
profile.set_preference("browser.download.manager.closeWhenDone", True);
profile.set_preference("pdfjs.disabled", True);

这篇关于在python中通过Selenium Webdriver下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆