简体   繁体   English

在python中通过Selenium Webdriver下载文件

[英]Downloading file through Selenium Webdriver in python

I am writing a program to automate web interaction through selenium webdriver in python.我正在编写一个程序,通过 python 中的 selenium webdriver 自动执行 web 交互。 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我使用以下代码来设置 Firefox 配置文件

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?在这种情况下,有人可以帮助我吗?

在此处输入图片说明

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.当您使用新的FirefoxProfile ,请使用set_preference方法以这种方式配置配置文件,以便单击“ Save和“ Ok ,并且不会在下载过程中中断。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM