how to change file download location in Webdriver while using chrome driver/firefox driver

后端 未结 7 601
太阳男子
太阳男子 2020-11-30 07:42

I am trying to save an image by using save as option inside a specific folder. I found a way by which I am able to right click on the image which I want to save using save a

相关标签:
7条回答
  • 2020-11-30 08:05

    I spent a lot of time to investigate how to download pdf file in firefox browser without Save As popup appearance. It migth be help someone.

    After some local investigation, how to download pdf file in firefox without any Save As popup, I found the minimum required preference in firefox profile:

    profile.setPreference("pdfjs.disabled", true);
    profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
    

    Of course you can add some additional preferences.

    It works in Firefox 45-46 versions.

    0 讨论(0)
  • 2020-11-30 08:06

    You partially answered your own question:

    the problem where i am stuck is after getting the os window

    Selenium is a browser automation tool - os window is not a browser! You will need to use something else. There are many choices, depending on your needs: Sikuli, Robot, AutoIt, ...

    0 讨论(0)
  • 2020-11-30 08:07

    Probably not the best solution but you could try using sikuli api to confirm the save for the box that shows up.

    The save as box is an OS window.

    0 讨论(0)
  • 2020-11-30 08:10

    For Chrome, it will works

    String downloadFilepath = "/path/to/download";
    HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
    chromePrefs.put("profile.default_content_settings.popups", 0);
    chromePrefs.put("download.default_directory", downloadFilepath);
    ChromeOptions options = new ChromeOptions();
    options.setExperimentalOption("prefs", chromePrefs);
    WebDriver driver = new ChromeDriver(options);
    
    0 讨论(0)
  • 2020-11-30 08:12

    Use the same Robot class and press enter to select the "Save" in the Windows dialog box.

    robo.keyPress(KeyEvent.VK_ENTER); robo.keyRelease(KeyEvent.VK_ENTER);

    if you need to rename it copy the file name in the clipboard and pass like below

    StringSelection file = new StringSelection("D:\\image.jpg");
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(file, null);
    
    Robot rb = new Robot();
    
    rb.setAutoDelay(2000); // Similar to thread.sleep
    
    rb.keyPress(KeyEvent.VK_CONTROL);
    rb.keyPress(KeyEvent.VK_V);
    
    rb.keyRelease(KeyEvent.VK_CONTROL);
    rb.keyRelease(KeyEvent.VK_V);
    
    rb.setAutoDelay(2000);
    
    rb.keyPress(KeyEvent.VK_ENTER);
    rb.keyRelease(KeyEvent.VK_ENTER);
    
    0 讨论(0)
  • 2020-11-30 08:25

    There are two things that are going wrong in code.

    For Firefox: You need to set

    profile.setPreference("browser.download.dir", "C:\\Users\\Admin\\Desktop\\ScreenShot\\");
    

    not to

    profile.setPreference("browser.download.dir", "C:\\Users\\Admin\\Desktop\\ScreenShot\\pic.jpeg");
    

    secondly, you are setting preference browser.download.folderlist, it is browser.download.folderList (L caps in folderList).

    Once you have achieved this both, you can use then your Robot class to perform desired operations.

    For Chromedriver try out with:

    String downloadFilepath = "/path/to/download";
    HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
    chromePrefs.put("profile.default_content_settings.popups", 0);
    chromePrefs.put("download.default_directory", downloadFilepath);
    ChromeOptions options = new ChromeOptions();
    options.setExperimentalOption("prefs", chromePrefs);
    DesiredCapabilities cap = DesiredCapabilities.chrome();
    cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
    cap.setCapability(ChromeOptions.CAPABILITY, options);
    WebDriver driver = new ChromeDriver(cap);
    

    Hope this helps. :)

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