Windows弹出式交互,用于在python中使用Selenium Webdriver下载 [英] Windows popup interaction for downloading using selenium webdriver in python
问题描述
我正在制作一个程序,使用python中的Selenium Webdriver自动下载数据.当我点击弹出窗口后的下载"按钮
I am making a programme to automatically download the data using selenium webdriver in python. When i click on "download" button following popup occours
.
,默认选项为打开方式".我希望程序首先单击选项保存文件",然后单击确定". 我已经使用以下代码来设置Firefox配置文件
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/xlsx")
但是对于我来说,它不起作用.然后,我尝试通过使用以下代码
But it is not working in my case. Then I tried to switch to this window from main window by using following code
parent_h = driver.current_window_handle
handles = driver.window_handles
handles.remove(parent_h)
driver.switch_to_window(handles.pop())
但是现在我不知道如何与该窗口进行交互?
But now I am not getting how to interact with this window?
推荐答案
经过大量的发现和研究,我得到了以下代码,对于这种情况有帮助.
After so much findings and research, I get the following code which would be helpful for this type of situation.
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);
这篇关于Windows弹出式交互,用于在python中使用Selenium Webdriver下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!