How do I automatically download files from a pop up dialog using selenium-python

后端 未结 10 1330
死守一世寂寞
死守一世寂寞 2020-12-16 13:19

I am trying to automatically download files from a pop up dialog using selenium-python.

The firefox popups look like this

相关标签:
10条回答
  • 2020-12-16 13:58

    Most browsers (in mine case Firefox) select the OK button by default. So I managed to solve this by using the following code. It basically presses enter for you and the file is downloaded.

    Robot robot = new Robot();
    
    // A short pause, just to be sure that OK is selected
    Thread.sleep(3000);
    
    robot.keyPress(KeyEvent.VK_ENTER);
    
    0 讨论(0)
  • 2020-12-16 13:59

    You have two options :

    1) Create a custom firefox profile with settings where the download location is pre-decided and firefox does not ask for confirmation to download. Just googled and found a blog that explains how to do it

    2) Use sikuli to automate clicks on the download dialog box. Blog explaining- How to use Sikuli

    P.S. - Not read the blogs, but I am sure they will give u a clue.

    0 讨论(0)
  • 2020-12-16 14:01

    I spent some time today trying to figure this out (again... had the solution at home but couldn't get to it...) and during that I found this... None of the solutions helped me so I thought I'd offer up what I did to solve this problem.

    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")
    
    0 讨论(0)
  • 2020-12-16 14:02

    With my using and test in my Selenium UI automation test, configuring the Firefox Profile is more stable than Robot Class. E.g. Disable popping up the System non-webpage Download/Save Dialog.

    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");
    
    0 讨论(0)
  • 2020-12-16 14:05

    The above solutions are great. But unfortunately my target file to download is a rare file type not in iana.org/assignments/media-types/media-types.xhtml. My solution is:

    1. Click on the download link in Firefox. Choose "Save File" and "Do this automatically for files like this from now on". This will create a a new "content type" in the Firefox profile when handling download.

    2. Locate and copy your Firefox profile (https://support.mozilla.org/en-US/kb/back-and-restore-information-firefox-profiles). Named the profile folder as "prepared_firefox_profile".

    3. Read in the custom profile:
    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)
    
    0 讨论(0)
  • 2020-12-16 14:10

    public class DemoFileDownload{

    FirefoxProfile prof = new FirefoxProfile(); prof.setpreference("browser.helperApps.neverAsk.SaveToDisk", "mimetype_of_file"); prof.setpreference("browser.download.folderlist",int_value); prof.setpreference("browser.download.dir,"folder_path"); // if the above int_value is 2 //int_value can be of below values: // 1 - downloads folder // 0 - desktop // 2 - custom folder } get the mime type from below website: www.sitepoint.com/mimetypes-complete-list/

    for chrome browser, use chromeoptions instead of firefoxprofile

    0 讨论(0)
提交回复
热议问题