banner



How To Handle File Upload Window In Selenium Webdriver Java

In this tutorial, we will learn How to deal with file uploads and downloads.

Uploading Files

For this section, nosotros will use http://demo.guru99.com/test/upload/ equally our test awarding. This site easily allows any visitor to upload files without requiring them to sign upwards.

Uploading files in WebDriver is done by but using the sendKeys() method on the file-select input field to enter the path to the file to be uploaded.

Handle File upload popup in Selenium Webdriver

handle file upload popup in selenium webdriver

Let's say we wish to upload the file "C:\newhtml.html". Our WebDriver code should be like the one shown below.

package newproject; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; public class PG9 {     public static void main(String[] args) {         Organization.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe");         String baseUrl = "http://demo.guru99.com/examination/upload/";         WebDriver commuter = new FirefoxDriver();          driver.get(baseUrl);         WebElement uploadElement = driver.findElement(By.id("uploadfile_0"));          // enter the file path onto the file-selection input field         uploadElement.sendKeys("C:\\newhtml.html");          // bank check the "I accept the terms of service" bank check box         driver.findElement(By.id("terms")).click();          // click the "UploadFile" button         driver.findElement(By.name("send")).click();         } }          

After running this script, yous should be able to upload the file successfully and you should become a message similar to this.

Remember following two things when uploading files in WebDriver

  1. At that place is no demand to simulate the clicking of the "Scan" push button. WebDriver automatically enters the file path onto the file-selection text box of the <input type="file"> chemical element
  2. When setting the file path in your Java IDE, use the proper escape character for the back-slash.

Downloading Files

WebDriver has no capability to access the Download dialog boxes presented by browsers when you click on a download link or button. However, we can bypass these dialog boxes using a carve up plan called "wget".

What is Wget?

Wget is a small and easy-to-use command-line program used to automate downloads. Basically, we will access Wget from our WebDriver script to perform the download process.

Setting up Wget

Pace one: In your C Drive, create a new folder and name it as "Wget".

Download wget.exe from here and Place it in the Wget folder yous created from the step above.

Step ii: Open Run by pressing windows fundamental + "R" ; type in "cmd & click ok

Blazon in the control "cd /" to motility to the root directory

Step 3: Type in the command to bank check whether the given setup is working

cmd /c C:\\Wget\\wget.exe -P C: --no-check-certificate http://demo.guru99.com/selenium/msgr11us.exe

In that location seems to be an consequence writing into C drive.

Pace 4: You need to debug the wget errors in command line before you lot execute the code using Selenium Webdriver. These errors will persist in Eclipse and the error messages will not be as informative. All-time to starting time get wget working using control line. If it works in command line information technology will definitely work in Eclipse.

In our example, every bit show in step 3, there is a problem writing into C drive. Let's change the download location to D drive and check results.

cmd /c C:\\Wget\\wget.exe -P D: --no-cheque-certificate http://demo.guru99.com/selenium/msgr11us.exe

Messenger was downloaded successfully.

Earlier you proceed further don't forget to delete the downloaded file

Using WebDriver and Wget

In the following example, we will employ WebDriver and wget to download a popular chat software chosen Yahoo Messenger. Our base of operations URL shall be http://demo.guru99.com/test/yahoo.html.

Stride 1

Import the "java.io.IOException" package because we will have to catch an IOException later in Step 4.

Step 2

Use getAttribute() to obtain the "href" value of the download link and relieve it equally a String variable. In this case, nosotros named the variable equally "sourceLocation".

Step 3

Set-upwards the syntax for wget using the post-obit command.

Pace 4

Initiate the download process by calling wget from our WebDriver code.

To sum it all up, your WebDriver lawmaking could look like the ane shown beneath.

packet newproject; import coffee.io.IOException;  import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; public class PG8 {     public static void main(Cord[] args) {                  System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe");                 String baseUrl = "http://demo.guru99.com/test/yahoo.html";         WebDriver driver = new FirefoxDriver();          driver.get(baseUrl);         WebElement downloadButton = commuter.findElement(By         .id("messenger-download"));         String sourceLocation = downloadButton.getAttribute("href");         Cord wget_command = "cmd /c C:\\Wget\\wget.exe -P D: --no-check-certificate " + sourceLocation;          try {         Process exec = Runtime.getRuntime().exec(wget_command);         int exitVal = exec.waitFor();         System.out.println("Leave value: " + exitVal);         } catch (InterruptedException | IOException ex) {         System.out.println(ex.toString());         }         driver.close();         }          }          

Later on executing this code, check your D drive and verify that the Yahoo Messenger installer was successfully downloaded there.

Summary

  • Uploading files in WebDriver is washed by but using the sendKeys() method on the file-select input field to enter the path to the file to be uploaded.
  • WebDriver cannot automate downloading of files on its own.
  • The easiest way to download files using WebDriver is to use Wget.

Source: https://www.guru99.com/upload-download-file-selenium-webdriver.html

Posted by: kistlercaude1987.blogspot.com

0 Response to "How To Handle File Upload Window In Selenium Webdriver Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel