What is a generic way to use explicit wait for each element in Selenium C#?
Waits help the user to troubleshoot issues while re-directing to different web pages. This is achieved by refreshing the entire web page and re-loading the new web elements. At times, there can be Ajax calls as well. Thus, a time lag can be seen while reloading the web pages and reflecting the web elements.
Users are often found navigating through various web pages back and forth. Thus, navigate() commands/methods provided by the WebDriver helps the user to simulate the real time scenarios by navigating between the web pages with reference to the web browser’s history for more selenium online training.
Why Do You Need Waits In Selenium?
Most of the web applications are developed using Ajax and Javascript. When a page is loaded by the browser the elements which we want to interact with may load at different time intervals. With this, it not only becomes difficult to identify the element but also if the element is not located, it will throw an “ElementNotVisibleException” exception. By using Waits, we can resolve this problem.
Now let’s move further and understand different types of waits.
Types of Waits
Selenium supports two types of waits and they are as follows
- Implicit Wait
- Explicit Wait
Note: The most widely used waits are Implicit and Explicit waits and Fluent waits are not preferable for real-time projects.
First, let’s understand what are Implicit waits in Selenium online training Hyderabad.
Implicit Waits
The Implicit wait will tell the web driver to wait for a certain amount of time before it throws a “No Such Element Exception”. The default setting of Implicit wait is zero. Once you set the time, the web driver will wait for that particular amount of time before throwing an exception.
Syntax: driver.manage().timeouts().implicitlyWait(TimeOut, TimeUnit.SECONDS);
Let’s take an example of Implicit waits and understand how it works.
package Edureka;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class ImplicitWait{
public static void main(String[] args) throws InterruptedException{
System.setProperty(“webdriver.chrome.driver”, “C:Selenium-java-edurekachromedriver_win32chromedriver.exe”);
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS); // pageload timeout
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); // Implicit Wait for 20 seconds
driver.get(“https://login.yahoo.com/");
driver.findElement(By.xpath(“//input[@id=’login-username’]”)).sendKeys(“[email protected]”); //Finding element and sending values
Thread.sleep(1000);
driver.findElement(By.xpath(“//input[@id=’login-signin’]”)).click(); //Clicking on the next button if element is located
}
}
In the above code, I have given Implicit Wait as 20 seconds, which implies the maximum wait time is 20 seconds for the particular element to load or to arrive at the output.
This was all about Implicit Waits. Now dive deeper into waits and understand what are explicit waits.
Explicit Waits
It is a concept of the dynamic wait which waits dynamically for specific conditions. It can be implemented by WebDriverWait class. To understand the Explicit wait in selenium online training India, you should know the requirement why we use wait statement in programs. I will give you a couple of examples in which you will get the complete idea of why waits in selenium are important.
Conditions for Explicit wait in selenium web driver
Suppose I have a web page which has some login form and after login, it takes a lot of time to load Account page or Home page. This page is dynamic it means sometimes it takes 10 seconds to load the homepage, sometimes it’s 15 second and so on. In such situations, Explicit wait helps us to wait until a specific page is not present.
You are working on travel application and you have filled a web form and clicked on the submit button. Now you have to wait until the specific data is not displayed. In this case, again you can use Explicit wait in which you can give waits till specific or set of elements are not displayed.
Syntax: WebDriverWait wait=new WebDriverWait(WebDriveReference,TimeOut);
In the above syntax, I have created an object of WebDriver wait and passed driver reference and timeout as parameters. This is how you need to write Explicit Waits. Now let’s take an example and understand how explicit wait works. Let’s take a look at the code below.
package
onlineitguru;
import
java.util.concurrent.TimeUnit;
import
org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.chrome.ChromeDriver;
import
org.openqa.selenium.support.ui.ExpectedConditions;
import
org.openqa.selenium.support.ui.WebDriverWait;
public
class
Locators {
public
static
void
main(String[] args) throws
InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:Selenium-java-edurekachromedriver_win32chromedriver.exe");
WebDriver driver = new
ChromeDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://www.facebook.com/");
WebElement firstname= driver.findElement(By.name("firstname"));
WebElement lastname= driver.findElement(By.name("lastname"));
sendKeys(driver, firstname, 10, "Edureka");
sendKeys(driver, lastname, 20, "Edureka");
WebElement forgotAccount= driver.findElement(By.linkText("Forgotten account?"));
clickOn(driver,forgotAccount, 10);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
public
static
void
sendKeys(WebDriver driver1, WebElement element, int
timeout, String value){
new
WebDriverWait(driver1, timeout).until(ExpectedConditions.visibilityOf(element));
element.sendKeys(value);
}
public
static
void
clickOn(WebDriver driver1, WebElement element, int
timeout){
new
WebDriverWait(driver1, timeout).until(ExpectedConditions.elementToBeClickable(element));
element.click();
}
}
In the above example, I have used facebook sign up credentials and located them using name locators. Further, I have created a utility or one generic function which will be available for all elements to provide Explicit wait. In the above example, I have written my own sendKeys() method. This method will enter the value in a particular text field, but internally it will provide explicitly wait also. Inside the sendKeys() method, I have given Expected Conditions for visibility of Element. i.e. I am asking the driver to wait for 20 seconds until expected condition visibility of the element. Further, if the condition is satisfied then you can apply sendKeys() to the method. Now, say I want to enter my first and last name. What I will do is, I will use this sendKeys() method and pass the driver, firstname, timeout i.e. 10 seconds and value as selenium online courses. Same goes with lastname as well.
When you execute the program, the chrome driver will launch Google Chrome and navigate through facebook.com and enter the values mentioned in the code. It’s not mandatory to set explicitly wait for a timeout of a particular value, based on your requirement you can change it. This is the major advantage of using explicit wait, but for implicitly wait, once you have defined 10 seconds it will be applicable to all the elements on the webpage and cannot be modified. Same goes with clickOn() method as well. But, this method is useful only for links on the webpage. This is how you can use Explicit Waits.
Note: Implicit, Explicit and Fluent waits, are dynamic waits. What are dynamic waits? Consider a situation where you have given TimeOut value as 20 seconds. If the element is loaded in 5 seconds, then rest 15 seconds will be ignored. It won’t wait till TimeOut value is completed i.e 20 seconds. That’s why all waits are considered as dynamic waits.
Let’s move further and differentiate between Implicit and Explicit Waits.
I hope you understood the difference between Implicit and Explicit Waits. This brings us to the end of the blog on Waits in Selenium training.
If you wish to learn Selenium and build a career in the testing domain, then check out our interactive, live-online selenium testing course online here, that comes with 24*7 support to guide you throughout your learning period.
Got a question for us? Please mention it in the comments section of Waits in Selenium blog and we will get back to you.