【问题标题】:How do I automatically download files from a pop up dialog using selenium-python如何使用 selenium-python 从弹出对话框中自动下载文件
【发布时间】:2014-09-11 05:27:04
【问题描述】:

我正在尝试使用 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 selenium


    【解决方案1】:

    在 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 文件,请根据您正在下载的任何文件类型对其进行修改。

    【讨论】:

    • Chrome 怎么样?我尝试了代码options = Options() options.add_argument('--disable-download-notification') self.driver = webdriver.Chrome(chrome_options=options) saveas = ActionChains(self.driver).key_down(Keys.CONTROL).send_keys('s').key_up(Keys.CONTROL) saveas.perform(),但它打开了另存为对话框
    • 如果您的文件类型不是 CSV,请参阅 MIME 类型列表,例如这个。 iana.org/assignments/media-types/media-types.xhtml
    【解决方案2】:

    基于 Amey 的回答 1),当然 Yi Zeng's blog (in ruby) 引用 Selenium 本身不会与像这样的系统级对话框以及 documentation 交互,这里是解决问题的 python sn-p

    p>
    from selenium import webdriver
    from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
    
    profile = FirefoxProfile()
    profile.set_preference("browser.helperApps.neverAsk.saveToDisk", 'application/pdf')
    driver = webdriver.Firefox(firefox_profile=profile)
    
    driver.get(target_url)
    #specific to target_url
    driver.find_element_by_css_selector('a[title="Click to Download"]').click()
    

    【讨论】:

      【解决方案3】:

      我发现了一个有用的解决方案,希望能帮助到那里的人。

      如果您通过按住 ALT 键单击开始下载,则可以使用 firefox 完全跳过提示。

      from selenium.webdriver.common.keys import Keys
      from selenium.webdriver.common.action_chains import ActionChains
      ...
      profile.set_preference("browser.altClickSave", True)
      ...
      element = driver.find_element_by_xpath("//a")
      ActionChains(driver).key_down(Keys.ALT).click(element).key_up(Keys.ALT).perform()
      

      这应该下载任何文件而不需要指定任何 MIME 类型。

      【讨论】:

      • 文件保存在哪里?
      • 也许这只适用于指向实际文件的超链接,但我不能让它在以不同方式触发下载但产生相同对话框的按钮上工作。比如说,与上面相同的代码,但使用element = driver.find_element_by_id('download_pdf_data-button')。对话框仍会显示。
      【解决方案4】:

      我今天花了一些时间试图解决这个问题(再次......在家里有解决方案但无法解决......)在此期间我发现了......没有任何解决方案对我有帮助我想我会提供我为解决这个问题所做的工作。

      from selenium import webdriver
      from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
      
      dl_path = "/tmp/"
      profile = FirefoxProfile()
      profile.set_preference("browser.download.folderList", 2)
      profile.set_preference("browser.download.manager.showWhenStarting", false)
      profile.set_preference("browser.download.dir", dl_path)
      profile.set_preference("browser.helperApps.neverAsk.saveToDisk",
                                "text/plain,text/x-csv,text/csv,application/vnd.ms-excel,application/csv,application/x-csv,text/csv,text/comma-separated-values,text/x-comma-separated-values,text/tab-separated-values,application/pdf")
      

      【讨论】:

        【解决方案5】:

        你有两个选择:

        1) 创建一个自定义的 firefox 配置文件,其中设置下载位置已预先确定,并且 firefox 不要求确认下载。 刚刚用谷歌搜索并找到了一个 blog,它解释了如何做到这一点

        2) 使用sikuli 自动点击下载对话框。 Blog解释-如何使用Sikuli

        附: - 不看博客,但我相信他们会给你一个线索。

        【讨论】:

        【解决方案6】:

        通过我在 Selenium UI 自动化测试中的使用和测试,配置 Firefox Profile 比 Robot Class 更稳定。例如。禁止弹出系统非网页下载/保存对话框。

        FirefoxProfile prof = new FirefoxProfile();
        
        ffprofile.setPreference("browser.download.panel.shown", false);
        ffprofile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/zip");
        
        //ffprofile.setPreference("browser.download.folderList", 1);  // Default to /home/user/Downloads in Linux.
        ffprofile.setPreference("browser.download.folderList", 2); 
        ffprofile.setPreference("browser.download.dir", "/tmp");
        

        【讨论】:

          【解决方案7】:
          FirefoxProfile fxProfile = new FirefoxProfile();
           fxProfile.SetPreference("browser.download.panel.shown", false);
           fxProfile.SetPreference("browser.helperApps.neverAsk.openFile", "text/csv,application/vnd.ms-excel");
           fxProfile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv,application/vnd.ms-excel");
           fxProfile.SetPreference("browser.download.folderList", 2); 
           fxProfile.SetPreference("browser.download.dir", "c:\\mydownloads");
           IwebDriver driver = new FirefoxDriver(fxProfile);
          

          【讨论】:

            【解决方案8】:

            大多数浏览器(在我的情况下是 Firefox)默认选择确定按钮。所以我设法通过使用以下代码解决了这个问题。它基本上为您按下回车键并下载文件。

            Robot robot = new Robot();
            
            // A short pause, just to be sure that OK is selected
            Thread.sleep(3000);
            
            robot.keyPress(KeyEvent.VK_ENTER);
            

            【讨论】:

            • 你从哪里得到Robot
            • 导入 java.awt.Robot;
            • 这个问题被标记为python
            【解决方案9】:

            公共类 DemoFileDownload{

            FirefoxProfile 教授 = 新的 FirefoxProfile(); prof.setpreference("browser.helperApps.neverAsk.SaveToDisk", "mimetype_of_file"); prof.setpreference("browser.download.folderlist",int_value); prof.setpreference("browser.download.dir,"folder_path"); // 如果上面的 int_value 是 2 //int_value 可以是以下值: // 1 - 下载文件夹 // 0 - 桌面 // 2 - 自定义文件夹 } 从以下网站获取 mime 类型: www.sitepoint.com/mimetypes-complete-list/

            对于 chrome 浏览器,使用 chromeoptions 而不是 firefoxprofile

            【讨论】:

              【解决方案10】:

              以上解决方案都很棒。但不幸的是,我要下载的目标文件是一种罕见的文件类型,不在 iana.org/assignments/media-types/media-types.xhtml 中。 我的解决方案是:

              1. 在 Firefox 中单击下载链接。选择“保存文件”和“从现在开始自动为此类文件执行此操作”。这将在处理下载时在 Firefox 配置文件中创建一个新的“内容类型”。

              2. 找到并复制您的 Firefox 配置文件 (https://support.mozilla.org/en-US/kb/back-and-restore-information-firefox-profiles)。将配置文件文件夹命名为“prepared_firefox_profile”。

              3. 读入自定义配置文件:
              profile = FirefoxProfile("dir/to/your/firefox/profile/prepared_firefox_profile")
              profile.set_preference("browser.download.dir", "/your/desired/download/folder")  
              driver = selenium.webdriver.Firefox(firefox_profile=profile)
              

              【讨论】:

                【解决方案11】:

                第一次勾选“从现在开始自动对此类文件执行此操作”小框后,不再弹出。

                【讨论】:

                • 简单但聪明,但我没有Do this automatically for files like this from now on复选框
                【解决方案12】:

                我使用以下 handlers.jsonuser.js 首选项下载 PDF:

                {
                    "defaultHandlersVersion": {},
                    "mimeTypes": {
                        "application/pdf": {
                            "action": 0,
                            "extensions": [
                                "pdf"
                            ]
                        }
                    }
                }
                
                //
                user_pref("browser.download.folderList", 2);
                user_pref("browser.download.manager.showWhenStarting", false);
                user_pref("browser.download.dir", "/absolute/path/to/target/folder");
                user_pref("browser.helperApps.alwaysAsk.force", false);
                user_pref("browser.download.manager.alertOnEXEOpen", false);
                user_pref("browser.download.manager.focusWhenStarting", false);
                user_pref("browser.download.manager.useWindow", false);
                user_pref("browser.download.manager.showAlertOnComplete", false);
                user_pref("browser.download.manager.closeWhenDone", false);
                
                user_pref("browser.download.viewableInternally.previousHandler.alwaysAskBeforeHandling.pdf", false);
                user_pref("browser.download.viewableInternally.previousHandler.preferredAction.pdf", 0);
                
                user_pref("pdfjs.migrationVersion", 2);
                

                【讨论】:

                  【解决方案13】:

                  您可能有这个 firefox 配置文件,在 Python 中您可以:

                  profile = FirefoxProfile()
                  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", "application/msword, application/csv, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream");
                  profile.set_preference("browser.download.manager.showWhenStarting", False);
                  profile.set_preference("browser.download.manager.alertOnEXEOpen", False);
                  profile.set_preference("browser.download.manager.focusWhenStarting", False);
                  profile.set_preference("browser.download.folderList", 2);
                  profile.set_preference("browser.download.useDownloadDir", True);
                  profile.set_preference("browser.helperApps.alwaysAsk.force", False);
                  profile.set_preference("browser.download.manager.alertOnEXEOpen", False);
                  profile.set_preference("browser.download.manager.closeWhenDone", True);
                  profile.set_preference("browser.download.manager.showAlertOnComplete", False);
                  profile.set_preference("browser.download.manager.useWindow", False);
                  profile.set_preference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", False);
                  profile.set_preference("pdfjs.disabled", True);
                  profile.set_preference("browser.download.dir", "C:\\Users\\***\\****\\Desktop\\Automation")
                  driver = webdriver.Firefox(firefox_profile = profile, executable_path = "Full file path to gecko driver.exe")
                  

                  【讨论】:

                    猜你喜欢
                    • 2012-11-17
                    • 2020-05-28
                    • 1970-01-01
                    • 1970-01-01
                    • 2020-08-31
                    • 2022-01-21
                    • 2021-06-25
                    • 2018-07-08
                    相关资源
                    最近更新 更多