【问题标题】:Downloading file through Selenium Webdriver in python在python中通过Selenium Webdriver下载文件
【发布时间】:2017-11-04 15:46:43
【问题描述】:

我正在编写一个程序来通过 python 中的 selenium webdriver 自动执行 Web 交互。当我通过脚本单击“下载”按钮时,我卡在了最后一步,屏幕上会弹出一个窗口,并选择默认选项“打开方式”。我希望我的程序首先单击“保存文件”选项,然后单击“确定”。我使用以下代码来设置 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")

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

如果选项“从现在开始自动为这样的文件执行此操作”是可点击的(通过复选框),那么上面的代码可以完美运行,但是当相同的选项不可点击时(如下图所示)然后在上面设置配置文件的代码失败。 在这种情况下谁能帮助我?

【问题讨论】:

    标签: python selenium selenium-webdriver webdriver firefox-profile


    【解决方案1】:

    当您使用新的FirefoxProfile 时,请使用set_preference 方法配置配置文件,以便单击SaveOk 并且它不会在下载过程中被中断。您可以按如下方式设置配置:

    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);
    

    【讨论】:

    • 感谢您的回答,但不幸的是,再次出现与(第二张图片中)相同的弹出窗口。我需要对其进行一些更改还是您有任何其他解决方案
    • 是的,一定要带上你的 tym。
    • 再次出现相同的弹出窗口。我不明白为什么会这样。据您所知,这是否与不可点击的选项“Do this.....”有关,这与我在问题中提到的相同
    • 是的,伙计,现在它可以工作了......非常感谢@DebanjanB。您能否提供我可以从中学习此配置文件设置步骤的来源...
    • @user8023364 很高兴能为您提供帮助 :) 您帮助他人越多,您在研发中学到的东西就越多。谢谢
    猜你喜欢
    • 1970-01-01
    • 2016-04-26
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多