
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Selenium with C Sharp - How to perform Explicit Wait method?
We can perform explicit wait with Selenium webdriver in C#. This is done to achieve synchronization between our tests and the elements on the page. For implementation of explicit wait, we have to take the help of the WebDriverWait and ExpectedCondition classes.
We shall create an object of the WebDriverWait class. The webdriver waits tillspecified wait time waiting for the expected condition for an element is satisfied.
After the time has elapsed, an exception is raised by Selenium.
The explicit waits are dynamic in nature which means if we have an explicit wait of five seconds and the expected condition is met at the third second, then the webdriver shall move to the next step immediately. It does not wait till the entirefive seconds.
Some of the expected conditions are −
UrlToBe
VisibilityOfAllElementsLocatedBy
UrlContains
AlertIsPresent
AlertState
ElementToBeSelected
ElementIsVisible
ElementExists
ElementSelectionStateToBe
ElementToBeClickable
InvisibilityOfElementWithText
InvisibilityOfElementLocated
TextToBePresentInElementLocated
TextToBePresentInElementValue
TextToBePresentInElement
StalenessOf
TitleContains
FrameToBeAvailableAndSwitchToIt
PresenceOfAllElementsLocatedBy
TitleIs
Syntax
WebDriverWait w = new WebDriverWait(driver, TimeSpan.FromSeconds(20)); w.Until(ExpectedConditions.ElementIsVisible(By.TagName("h1")));
Let us try to wait for the text - Team @ Tutorials Point to be visible on the page −
Example
using NUnit.Framework; using OpenQA.Selenium; using OpenQA.Selenium.Firefox; using System; using OpenQA.Selenium; using OpenQA.Selenium.Support.UI; namespace NUnitTestProject2{ public class Tests{ String url = "https://www.tutorialspoint.com/about/about_careers.htm"; IWebDriver driver; [SetUp] public void Setup(){ //creating object of FirefoxDriver driver = new FirefoxDriver(""); } [Test] public void Test2(){ //URL launch driver.Navigate().GoToUrl(url); //identify element then click IWebElement t = driver.FindElement(By.XPath("//*[text()='Team']")); t.Click(); //expected condition of Element visibility WebDriverWait w = new WebDriverWait(driver, TimeSpan.FromSeconds(20)); w.Until (ExpectedConditions.ElementIsVisible(By.TagName("h1"))); //identify element then obtain text IWebElement n = driver.FindElement(By.TagName("h1")); Console.WriteLine("Text is: " + n.Text); } [TearDown] public void close_Browser(){ driver.Quit(); } } }
Output
- Related Questions & Answers
- What does explicit wait perform?
- What is the explicit wait in Selenium with python?
- Explain explicit wait in Selenium webdriver in Python.
- C Sharp with Selenium - How to Switch one tab to another tab in Csharp Selenium?
- What does implicit wait perform?
- What does fluent wait perform?
- What is implicit wait in Selenium with python?
- How to get Selenium to wait for ajax response?
- Best practice to wait for a change with Selenium Webdriver?
- Wait for complex page with JavaScript to load using Selenium.
- Make Selenium wait 10 seconds.
- C# and Selenium: Wait Until Element is Present
- Wait for an Ajax call to complete with Selenium 2 WebDriver.
- How to perform browser navigations in Selenium?
- How to define a single-dimensional array in C Sharp?