Welcome again!
Ever think of handling Windows Controls via Selenium?
Many times we come across the situation where we need to click on a button and a new pop up window appears, for instance, in case of File upload. We need to switch to that window which is either our drive or the local machine.
Selection a file from a local machine or drive does not belong to any web browser.
As we know, Selenium works with the only web-based application, so, in this blog, we are going to see how we can automate Windows Controls using selenium.
Types of Windows Controls
There are two types of Window controls :
1. Windows authentication pop-ups
Many companies put proxy settings to access servers. If you try to open the server, it asks for valid credentials and do log in to have access to that server. If we don’t handle the popup, we couldn’t do further testing
Please note that window pop-ups are NOT similar to javascript pop-ups. Simple authentication is shown below.

Simplest way to handle this is to append username and password in the site URL. It automatically enter the credentials that way.
Syntax
http://Username:Password@SiteURL
String authCred = "http://" + komal + ":" + Tester#123 + "@" + www.testing.com;
driver.get(authCred);
Alert alert = driver.switchTo().alert();
alert.accept();
2. Handling file upload from Windows
File upload in Web applications can be achieved by AutoIT.
What is AutoIT?
You might have come across the real-time scenarios where you need to upload a file using the “Choose File” button.
Basically, “Choose File” can be opened up using Selenium but the process after that is achieved using a handler called AutoIT.
SciTE is the editor we use to write our script in AutoIT.
Steps to download and install AutoIT for Windows
- First, Download AutoIT from here
- Simply, Run this set up file.
- Then, Install it by clicking next and go through all the installing process
- After that, A folder will be created with program file.
- Now, Open the AutoIT folder and find Scite editor
- Finally, in SciTE write the following code.



Code for Handling Windows Controls (Upload File) :
package com.knoldus.snap3;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
public class FileUpload {
public static void main(String[] args) throws InterruptedException, IOException {
// TODO Auto-generated method stub
String downloadPath=System.getProperty("user.dir");
System.setProperty("webdriver.chrome.driver", "/home/knoldus/Downloads/Automation/chromedriver_linux64/chromedriver");
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadPath);
ChromeOptions options=new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
WebDriver driver=new ChromeDriver(options);
driver.get("https://altoconvertpdftojpg.com/");
driver.findElement(By.cssSelector("[class*='btn--choose']")).click();
Thread.sleep(3000);
Runtime.getRuntime().exec("/home/knoldus/KomalCertificate.pdf");
WebDriverWait wait=new WebDriverWait(driver,10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("button[class*='medium']")));
driver.findElement(By.cssSelector("button[class*='medium']")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Download Now")));
driver.findElement(By.linkText("Download Now")).click();
Thread.sleep(5000);
File f=new File(downloadPath+"/converted.zip");
if(f.exists())
{
Assert.assertTrue(f.exists());
if(f.delete())
System.out.println("file deleted");
}
}
}
To find out How logs can be inserted in a selenium code click here.
References
https://stackoverflow.com/questions/37239766/how-to-handle-a-window-control-in-selenium
https://www.autoitscript.com/wiki/Tutorials


