เราสามารถรอจนกว่าองค์ประกอบจะปรากฏใน Selenium webdriver โดยใช้การรอที่ชัดเจน ส่วนใหญ่จะใช้เมื่อใดก็ตามที่มีปัญหาการซิงโครไนซ์เพื่อให้องค์ประกอบพร้อมใช้งานบนหน้า
คลาส WebDriverWait และ ExpectedCondition ใช้สำหรับการดำเนินการรออย่างชัดเจน เราต้องสร้างวัตถุของ WebDriverWait ซึ่งจะเรียกใช้เมธอดของคลาส ExpectedCondition
โปรแกรมควบคุมเว็บจะรอตามระยะเวลาที่กำหนดเพื่อให้ตรงตามเกณฑ์ที่คาดหวัง หลังจากเวลาผ่านไป ข้อยกเว้นจะถูกโยนทิ้งไป เพื่อรอให้องค์ประกอบปรากฏ เราต้องใช้เงื่อนไขที่คาดไว้ – ElementExists
ไวยากรณ์
WebDriverWait w = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
w.Until(ExpectedConditions.ElementExists(By.TagName("h1"))); ให้เราลองรอข้อความ - About Careers at Tutorials Point ในหน้า −

ตัวอย่าง
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();
}
}
} ผลลัพธ์
