Upload files with Selenium using AutoIt.

Results-driven SDET with expertise in automation frameworks, API testing, and CI/CD pipelines. Proficient in Selenium, Appium, Postman, JUnit, TestNG and Jenkins. Skilled in Java and performance testing, ensuring high-quality software delivery in Agile environments.
To upload files using Selenium and AutoIt, you need to combine the capabilities of both tools. Selenium handles browser interactions, while AutoIt handles system-level file upload dialogs that are not directly accessible through Selenium.
Description: To click on the “Choose File“ and upload a file.

Here's a step-by-step guide:
1. Install AutoIt
Download AutoIt from https://www.autoitscript.com/site/autoit/downloads/ .
Install it on system keeping x86 and x64 in mind.
2. Write the AutoIt Script
Create an AutoIt script to interact with the file upload dialog

// AutoIt script to interact with a file upload dialog
// Wait for the dialog to appear
Sleep(1000)
// Wait until the 'Open' dialog is active
WinWaitActive("Open")
// Set the file path to upload
Local $filePath = "C:\Path\To\Your\File.jpg"
// Focus on the "Edit1" text box in the "Open" dialog
ControlFocus("Open", "", "Edit1")
// Type the file path into the edit box
Sleep(1000)
ControlSetText("Open", "", "Edit1", $filePath)
// Click the Open button to upload the file
Sleep(1000)
ControlClick("Open", "", "Button1")

3. Compile the Script
Save the script with a
.au3extension (e.g.,FileUpload.au3).Compile it into an executable using the AutoIt Script Editor or the AutoIt Compiler (right-click the file and select "Compile Script").
This will generate an .exe file (e.g., FileUpload.exe).
4. Integrate with Selenium
Use Selenium to trigger the file upload dialog and then execute the AutoIt script.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.IOException;
public class FileUploadWithAutoIt {
public static void main(String[] args) {
// Set up the WebDriver
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
WebDriver driver = new ChromeDriver();
try {
// Open the target webpage
driver.get("https://example.com/upload");
// Click the upload button to open the file dialog
WebElement uploadButton = driver.findElement(By.id("uploadButtonId")); // Replace with the actual ID
uploadButton.click();
// Execute the AutoIt script
Runtime.getRuntime().exec("C:\\path\\to\\FileUpload.exe");
// Optional: Wait to observe the process
Thread.sleep(3000);
// Continue with other actions if needed
} catch (IOException | InterruptedException e) {
e.printStackTrace();
} finally {
// Close the browser
driver.quit();
}
}
}
Key Points to Remember
Replace
C:\\path\\to\\your\\file.txtin the AutoIt script with the actual file path you want to upload.Replace the Selenium
Bylocators with the actual locators of the file upload button on your web page.Ensure the
.exefile is located in the path specified in theRuntime.getRuntime().exec()method.
Conclusion:
By combining Selenium and AutoIt, you can automate file uploads effectively, even when handling OS-level file dialog boxes that Selenium cannot interact with directly. Selenium triggers the file upload, and AutoIt handles the dialog interaction seamlessly. This approach is especially useful for automating complex workflows involving native system dialogs.
🙌🙌 Connect with me on LinkedIn to stay up-to-date with my latest blog posts.

