Table of Contents
15 Advanced Selenium Webdriver Commands
Handling iframes
Inline frames that are used on webpage or web applications called iframes. They are generally used to insert HTML contents from external sources. While automating web pages, you may also have to automate elements within the iframes. Selenium Webdriver has some methods that can handle multiple iframes included on the web page.
1. Locating iframes by ID or Name
It is the most convenient way of locating iframes. For this, you have to identify the ID or Name of the frame at first. You can do this with inspecting the web page using web inspector tools. After that use the following selenium webdriver command for switching between iframes.
driver.switchTo().frame("frameID");
driver.switchTo().frame("frameName");
2. Locating iframes by Index
You may locate the iframe within the HTML document with specifying it’s index number as given below.
driver.switchTo().frame(0);
3. Locating iframe by tagName
If the iframe not used any attributes such as Id, Name etc. may difficult to locate it. So, you have to use
driver.switchTo().frame((WebElement) driver.findElements(By.tagName("iframe")).get(0));
Exception Handling
The conditions or situations that interrupt the programs from being executed smoothly called exceptions. So, exception handling prevents such situations while executing the program. Selenium webdriver also supports handling exceptions that may be introduced while executing webdriver scripts. Similar to other programming languages, exceptions can be handled using try and catch block as given below.
try {
//write here the test scripts for automation
}
catch(ExceptionName e)
{
//write the codes to catch the exceptions while running try block
}
You may also use multiple catch blocks as given below if the try block throws multiple exceptions having different types.
try {
//write here the test scripts for automation
}
catch(Exception1 e)
{
//write the codes to catch the exceptions while running try block
}
catch(Exception2 e)
{
//write the codes to catch the exceptions while running try block
}
Following is an implementation of using exception handling in selenium webdriver.
WebElement submitButton=driver.findElement(By.cssSelector("input[type=submit]"));
try {
if(submitButton.isDisplayed()){
submitButton.click();
}
}
catch(NoSuchElementException e) {
e.printStackTrace();
}
In the above code ‘NoSuchElementException‘ is used to catch the exceptions when using isEnabled(), isDisplayed(), and isSelected() methods. For this you have to import “import org.openqa.selenium.NoSuchElementException;” at first.
Handling Waits and Conditions
You may make the Webdriver stop executing the next line of code until specified time or conditions. Using these methods, you can set the page load time, waiting time before locating web element and wait until the expected conditions.
Following are some of the advanced selenium webdriver commands that can be used for handling waits.
1. PageLoadTimeOut(time,unit)
While automating the web page or web application, it may occur unlimited loading due to network or server error. This command allows you to set the time for loading the specified web page to avoid such conditions. The pageLoadTimeout() method usually follows a get() method as given below.
driver.get("www.siteforinfotech.com");
driver.manage().timeouts().pageLoadTimeout(500, TimeUnit.SECONDS);
2. implicitlyWait()
There is another method implicitlyWait() to make waits while automating the web page. It is generally used to wait certain amount of time before locating the element. Following is an example of using implicitlyWait() method.
driver.manage().timeouts().implicitlyWait(500,TimeUnit.SECONDS);
In order to execute above commands, you have to import the following package at first.
import.java.util.concurrent.Timeunit;
Following are some of the advanced selenium webdriver commands that can be used for handling conditions.
1. isEnabled()
This method is used to verify whether the specified element within the web page is enabled or not before executing
WebElement email=driver.findElement(By.name("your-email"));
if (email.isEnabled()){
email.sendKeys("ma**@te**.com");
}
2. isDisplayed()
Similar to isEnabled() method, isDisplayed() method verifies whether the certain element is displayed or not before executing the command.
WebElement submit=driver.findElement(By.id("submit"));
if(submit.isDisplayed()){
submit.click();
}
3. isSelected()
This method is used to verify whether the option on the element such as a checkbox, drop down option, radio button etc. is selected. Here the example of the uses of the
if (driver.findElement(By.id("option1").isSelected()){
driver.findElement(By.id("next").click();
}
Expected Conditions
Expected conditions are wider than the set of conditions for handling advanced selenium webdriver commands and they are used with until() method. You have to import the following packages before implementing the expected conditions.
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
Same as handling waits, the expected conditions may be until the alert present, the element to be clickable, the element is located etc.
1. Until the visibility of element located
You can use visibilityofElementLocated() method to explicitly wait until an element is visible on the web page. Following is an example of uses of the visibilityofElementLocated() method that waits up to 5 seconds before proceeding further.
WebDriverWait waitVar=new WebDriverWait(driver,5);
WebElement elt=waitVar.until(ExpectedConditions.visibilityOfElementLocated(By.name("your-name")));
elt.sendKeys("Your Name");
2. Until alerts presents
The alertsPresent() method with until() method explicitly waits until an alert appears on the web page. Following is an example of uses of the alertsPresent() method to wait up to 8 seconds when the
WebDriverWait waitVar=new WebDriverWait(driver,8);
waitVar.until(ExpectedConditions.alertIsPresent());
3. Until element to be clickable
The elementToBeClickable() method waits until an element is visible along with enabled for making the mouse click on it. Following is an example of the uses of elementToBeClickable() method.
WebDriverWait waitVar2=new WebDriverWait(driver, 5);
WebElement elt=waitVar.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[type=submit]")));
elt.click();
Handling Popup Windows
Selenium webdriver allows you to handle popup windows like alerts. SwitchTo().alert() method is used to move webdriver to the popup windows, you may access the elements such as alert text, buttons etc. within the popup window after switching to it. The getText() method can be used to retrieve message from alert box whereas switchTo().alert().accept() method automatically closes the popup window. Following is an example of switching between pop up windows.
String alertText=driver.switchTo().alert().getText();
driver.switchTo().alert().accept();
System.out.println(alertText);
Handling Cookies
Cookies are small files that are stored on the local computer that includes information submitted by the websites visited by a user. Selenium webdriver also able to perform different tasks for handling more advanced selenium webdriver commands such as browser cookies. Handling browser cookies are required for validating entries in cookie files while automating the webpage or web application.
1. Get Cookies
You can use the method getCookies() in order to get all the cookies for the current website which are visiting. Following is an example of using getCookies() method to get all the cookies from the
Set<Cookie> ExtractCookies=driver.manage().getCookies();
for (Cookie cookie:ExtractCookies){
System.out.println("cookie Name -"+cookie.getName()+"Cookie Value -"+cookie.getValue());
}
Along with getting all the cookies, you may get the cookie by specific name as the following method.
System.out.println(driver.manage().getCookieNamed("cookieName"));
2. Add Cookies
The addCookie() method allows you to add specific cookies into cookies for the current domain. Here is an example of using addCookie() method to add cookies.
Cookie cookie1=new Cookie("CookieName", "CookieValue");
driver.manage().addCookie(cookie1);
3. Delete Cookies
Since Selenium webdriver allows you to delete cookies in different ways. You may delete a particular cookie, delete the
In order to delete the particular cookie from the current domain, you can use
driver.manage().deleteCookieNamed("cookieName");
The deleteCookie() method allows you to deletethe
driver.manage().deleteCookie(cookie1);
You can use another method
driver.manage().deleteAllCookies();
Read Next: Handling Static & Dynamic Tables Using Selenium WebDriver