- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# and Selenium: Wait Until Element is Present
We can wait until an element is present in Selenium webdriver using the explicit wait. It is mainly used whenever there is a synchronization issue for an element to be available on page.
The WebDriverWait and the ExpectedCondition classes are used for an explicit wait implementation. We have to create an object of the WebDriverWait which shall invoke the methods of the ExpectedCondition class.
The webdriver waits for a specified amount of time for the expected criteria to be met. After the time has elapsed, an exception gets thrown. To wait for an element to be present, we have to use the expected condition – ElementExists.
Syntax
WebDriverWait w = new WebDriverWait(driver, TimeSpan.FromSeconds(20)); w.Until(ExpectedConditions.ElementExists(By.TagName("h1")));
Let us try to wait for the text - About Careers at Tutorials Point to be available 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 l = driver.FindElement(By.XPath("//*[text()='Careers']")); l.Click(); //expected condition of ElementExists WebDriverWait w = new WebDriverWait(driver, TimeSpan.FromSeconds(20)); w.Until(ExpectedConditions.ElementExists(By.TagName("h1"))); //identify element then obtain text IWebElement m = driver.FindElement(By.TagName("h1")); Console.WriteLine("Element text is: " + m.Text); } [TearDown] public void close_Browser(){ driver.Quit(); } } }
Output
- Related Articles
- How to wait until an element is present in Selenium?
- How to wait until an element no longer exists in Selenium?
- Need Selenium to wait until the document is ready
- Wait until page is loaded with Selenium WebDriver for Python.
- Need to wait until page is completely loaded - Selenium WebDriver
- Test if element is present using Selenium WebDriver?
- What is implicit wait in Selenium with python?
- Make Selenium wait 10 seconds.
- What is the explicit wait in Selenium with python?
- How to verify an attribute is present in an element using Selenium WebDriver?
- Selenium with C Sharp - How to perform Explicit Wait method?
- Verifying whether an element present or visible in Selenium Webdriver
- 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.

Advertisements