Thursday 1 August 2019

How to upload file in selenium - robot class and AutoIT

Upload file in selenium

This is the most frequent question how to upload file in selenium that we always have in selenium webdriver to upload files in upload section of a web page or a web form.We have mainly 2 method that we can use to upload file in web page.One method is Upload file in selenium webdriver using sendkeys and other is upload file in selenium webdriver using autoit.

How to upload a file using selenium webdriver in java

We will see how we can upload file in selenium webdriver using sendkeys.This is the most frequently used technique to upload file using sendkeys().This is the inbuild selenium method to send key values to textbox but here we will see how we can use this method to upload files in the upload element or upload section or browse button.


Upload file in selenium webdriver using sendkeys:

Below is the generic method that we will used to upload the files by just passing the file path as a parameter to the method and this method will find the upload element in the UI and send the path of the file to upload or upload to input type file. Below is the method which will be used to upload file in selenium

Method to upload file in selenium:
public static void main(String args[])
{ public void Upload_document_in_Selenium(String filepath)
{
delay(3);
int in=filepath.indexOf(".");
FilePaths=filepath.substring(0,in);
String path = System.getProperty("user.dir")+"\\TestData\\"+FilePaths;
    delay(3);
        UploadFile.sendKeys(path);
delay(10);
UploadButton.click();
delay(3);
clickNext.click();
}
}


Upload file in selenium,upload file in selenium webdriver using sendkeys


The above method will get the file path and upload the file using selenium sendkeys method, now if this does not work due to any limitation of the application or technology used in the web development. we can take the help of upload file in selenium webdriver using robot class or upload file in selenium webdriver using autoit.
We will see how we can upload file in selenium using autoit in next blog. Use this upload trick more efficiently in projects and happy testing



0 comments: