Upload File in Selenium using AutoIt

Profile picture for user arilio666

This article will try uploading a file using AutoIt, a Windows GUI automation tool using selenium. Autoit is used to automate windows dialogue.

Let us see how we can upload using this.

1. Download and install AutoIt

Download AutoIt from here.

2. Scripting SciTE script editor

  • Open SciTE script editor.
  • Open AutoIt application.
  • Navigate to the upload page and click the upload button to view the dialogue popup.
Upload
  • As shown using the finder tool navigate to the path space and note down the title, class, and instance.
  • Do the same for the open button in the popup box.
ControlFocus("Open","","Edit1")
ControlSetText("Open","","Edit1","C:\Users\arili\git\Assignments\Selenium\target\L.png")
ControlClick("Open","","Button1")

The script should be comprised of this.
ControlFocus points to the title, class, and instance of the path area box.
ControlSetText sets the image path we will upload using the title, class, and instance.
ControlClick is set using the finder tool for the open button to upload a file with its respective title, class, and instance.

Open Button
  • Please copy and paste the script into the SciTE script editor and save it to the desktop as filename.au3.
  • Right-click the au3 file and click on compile script to convert to .exe format for selenium to run.

3. Selenium

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import io.GitHub.bonigarcia.wdm.WebDriverManager;

public class FileUpload {

public static void main(String[] args) throws Exception {
 WebDriverManager.chromedriver().setup();
 ChromeDriver driver = new ChromeDriver();
 driver.get("http://autopract.com/selenium/upload1/");
 driver.manage().window().maximize();
 WebElement uploadButton = driver.findElement(By.xpath("//span[@class=\"btn btn-success fileinput-button\"]"));
 uploadButton.click();
 Thread.sleep(5000);
 Runtime.getRuntime().exec("C:/Users/arili/Desktop/Auto/Up.exe");


}

}
  • So after clicking add files, the windows dialogue box opens in selenium.
  • Using the runtime option in selenium, we will open the converted .exe file of the scITE script we just created to enter the fixed script path and click on the windows dialogue open button to upload.
Result
  • We can see that this method works flawlessly using AutoIt.