Thursday, June 27, 2024

Download Files in Chrome, Firefox, etc.

 

Download Files in Chrome, Firefox, etc.

Setting Download Location in Chrome:

To set a default download location in Chrome, follow these steps:

  1. Manual Settings:
    • Open Chrome.
    • Click the three dots (menu) in the top-right corner.
    • Select Settings.
    • In the search bar, type downloads.
    • Scroll to the bottom to see Downloads settings.
    • Under Location, you will see the default download path (e.g., C:\Users\Lenovo\Downloads).
    • You can change the download location by clicking Change.
    • To prompt where to save each file before downloading, enable Ask where to save each file before downloading.

 

Note:

We can change downloads location folder name .

 

 

2.     Automated Settings with Selenium:

  • To change download settings in automation, you need to configure ChromeOptions and FirefoxOptions.

 

 

DownLoad File in default Location:

 

package UploadfileBasics;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

 

public class DownloadsFileInDefaultLocation {

 

            public static void main(String[] args) {

                        System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

 

                        //                     //open chrome browser

                        WebDriver  driver =  new ChromeDriver();               

                        driver.get("https://www.w3schools.com/howto/tryit.asp?filename=tryhow_html_download_link2");

 

                        // click Logo

//                     driver.findElement(By.xpath("//img[@alt='W3Schools']")).click();

                                               

 

                        //selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//img[@alt='W3Schools']"}

 

                        // first Check that ele is in frame, if it is in frame, we have to  swicth control to frame..

                        //   by name or id  or indexno or Webele

                        // Switch to Frame

                        driver.switchTo().frame("iframeResult");

                       

                                    // click Logo

                                    driver.findElement(By.xpath("//img[@alt='W3Schools']")).click();

 

                        //   it downloads file in default location of chrome browser ????

 

                        // Switch control to main page

                        driver.switchTo().defaultContent();

                        System.out.println("ends");

 

            }

 

}

 

 

DownLoad File in Specific folder /Project Location:

 

// set  download location for Automation

                        HashMap<String, Object> hm = new HashMap<String, Object>();

 

                        // dont want popup window Where we need to save the file -- disable popup window - asking where to save the

                        hm.put("profile.default_content_settings.popups", 0);

 

                        // To download any file in the given location

                        hm.put("download.default_directory", "C:\\brahma\\MyDownloads");

//                     C:\brahma\MyDownloads

                       

 

---------------------------

                       

package UploadfileBasics;

 

import java.util.HashMap;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.chrome.ChromeOptions;

 

public class DownloadsFileInProjectLocation {

 

            public static void main(String[] args) {

            //  download  file in this folder--C:\brahma\Practise\SelniumPractiseNew\Jul2023WorkSpace\SeleniumProjectSep72023

                       

                                    // to get the current project  directory/folder location

                                    String ProjectDirectoryName = System.getProperty("user.dir");

                                   

                                    // To download any file in the 'current Project' location

                                    // get project folder path

 

                                    //   C:\brahma\Practise\SelniumPractiseNew\March52024MyWorkspace\SeleniumMay52024\src\Downloads

                                    System.out.println("ProjectDirectoryName="+ProjectDirectoryName);

                                    // C:\brahma\Practise\SelniumPractiseNew\March52024MyWorkspace\SeleniumMay52024

 

                                    // C:\brahma\Practise\SelniumPractiseNew\March52024MyWorkspace\SeleniumMay52024\Downloads

//                                 ProjectDirectoryName =  ProjectDirectoryName + "\\Downloads";

                                   

                                    // check

                                    // set  download location for Automation

                                    HashMap<String, Object> hm = new HashMap<String, Object>();

 

                                    // dont want popup window Where we need to save the file -- disable popup window - asking where to save the

                                    hm.put("profile.default_content_settings.popups", 0);

 

                                    // To download any file in the given location

//                                 hm.put("download.default_directory", "C:\\brahma\\MyDownloads");

//                                 C:\brahma\MyDownloads

//                                 hm.put("download.default_directory", "C:\\brahma\\MyDownloads");

                                    //                     hm.put("download.default_directory", ".\\Downloads"); // dont use  --  not working

//                                 hm.put("download.default_directory", ProjectDirectoryName);

                                    hm.put("download.default_directory", ProjectDirectoryName +"\\src\\UploadfileBasics");

//                                 hm.put("download.default_directory",".\\src\\UploadfileBasics");// not working

                                   

                                   

                                    ChromeOptions chOptions  = new ChromeOptions();

                                    // accept SSL error

//                                 chOptions.setAcceptInsecureCerts(true);

                                   

                                    chOptions.setExperimentalOption("prefs", hm);     

 

 

                                    System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

                                    WebDriver driver = new ChromeDriver(chOptions);

                                    //                     //open chrome browser by passing chromeoptions  ref var

 

                                    //  while opening chrome browser, it applies these setings

                                    //                                     modifies these settings in chrome browser

                                   

 

                                    driver.get("https://www.w3schools.com/howto/tryit.asp?filename=tryhow_html_download_link2");

 

                                    // click Logo

                                    //                     driver.findElement(By.xpath("//img[@alt='W3Schools']")).click();

                                    //selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//img[@alt='W3Schools']"}

                                    // first Check that ele is in frame, if it is in frame, we have to  swicth control to frame..

                                                            //   by name or id  or indexno or Webele

                                                            // Switch to Frame

                                                            driver.switchTo().frame("iframeResult");                 

                                                                        // click Logo

                                                            driver.findElement(By.xpath("//img[@alt='W3Schools']")).click();

 

                                                            //   it downloads file in default location of chrome browser

 

                                                            // Swicthc control to main page

                                                            driver.switchTo().defaultContent();

 

 

                                    System.out.println("ends");

 

 

            }

 

}

 

HW :  Download files in project folder in Edge browser?

 

HW   Download files in project folder in Firefox browser?

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...