In earlier articles, we saw that, we can upload the file using sendKeys() method or by using robot class.
Refer below articles for this.
Upload file in selenium using sendkeys() method.
Upload file in selenium using robot class
Now, in case both of the above methods won’t work, then we have to use AutoIT tool (which a third-party tool used to handle window popups). Let’s talk how this can be done.
AutoIt v3 is a freeware BASIC-like scripting language designed for automating Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement and window/control manipulation in order to automate tasks.
This method is for handling the Windows File Upload dialog, which cannot be handled using Selenium. Please follow below steps:
1. Download AutoIT setup file from here (https://www.autoitscript.com/site/autoit/downloads/)
Click here to download AutoIT.exe (https://www.autoitscript.com/cgi-bin/getfile.pl?autoit3/autoit-v3-setup.exe)
2. Open Au3Info.exe for which helps to find window information details.
3. Open SciTE Script Editor.
Script used to upload the file is as below. Write the script below in script editor.
ControlFocus(“File Upload”,””,”Edit1″)
ControlSetText(“File Upload”,””,”Edit1″,”C:UsersPrakashDesktopTOSCA tutorial.pptx”)
ControlClick(“File Upload”,””,”Button1″)
We can get Title and control id from application Au3Info.exe. Just Drag and drop the finder tool at required position.
In this script Control Focus is used to focus on the field where we enter the file to be uploaded path details. Note that Control id is the combination of Class and Instance.
Syntax – ControlFocus(“title”,”text”,”Control id”)
ControlSetText is used to write the text (file path of the file to be uploaded)
Syntax – ControlSetText(“title”,”text”,”Control id”,”file path”)
ControlClick is used to click on the button.
Syntax – ControlClick(“title”,”text”,”Control id”)
2. Save the Script with some name with .au3 extension.
3. Then right click on it, click on Compile.
4. Then Double click on the same file.
5. Now you will see the .exe file with same name is generated.
We are going to use this .exe file in our java – selenium program (to upload the file using AutoIT).
6. Use below piece of code for executing the .exe file created above
Runtime.getRuntime().exec(“C:UsersPrakashDesktopTestScript.exe”);
Refer below example –
package basicsOfSelenium;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class AutoIT {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://www.tinyupload.com/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
// Click on Browse Button
driver.findElement(By.xpath("html/body/table/tbody/tr[4]/td/table/tbody/tr/td[2]/form/table/tbody/tr[2]/td[1]/input[2]")).click();
//After pop-up open, below script is used to upload the file
Runtime.getRuntime().exec("C:UsersPrakashDesktopTestScript.exe");
}
Hope this helps !!!!