Wait for an element to load in selenium using C#

Sometimes while performing automation we have to wait for an element to load then only we can proceed to the next steps.
To achieve this selenium has provided explicit wait and implicit wait.
Here we will discuss about use of explicit wait .net using C#.
Using lambda Expression:
 Name space need to use to access WebDriverWait
Using OpenQA.Selenium.Support.UI
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
//add this name space to access WebDriverWait
using OpenQA.Selenium.Support.UI;
namespace AutomationUsingSelenium
{
    class Program
    {
        static void Main(string[] args)
        {
            var capabilitiesInternet = new OpenQA.Selenium.Remote.DesiredCapabilities();
            capabilitiesInternet.SetCapability(“ignoreProtectedModeSettings”, true);
          
            IWebDriver webDriver = new InternetExplorerDriver(capabilitiesInternet);
            webDriver.Navigate().GoToUrl(http://www.google.com”);
            IWebElement query = webDriver.FindElement(By.Name(“q”));
            query.SendKeys(http://seleniumdotnet.blogspot.com”);
            OpenQA.Selenium.Support.UI.WebDriverWait wait = new OpenQA.Selenium.Support.UI.WebDriverWait(webDriver, TimeSpan.FromSeconds(10));
            Console.WriteLine(“Page title is: “+webDriver.Title);
            //using lambda expression
            wait.Until(d => d.FindElement(By.Id(“ab_name”)));//make sure that Search div appear on google
            Screenshot screenshot = ((ITakesScreenshot) webDriver).GetScreenshot();
        screenshot.SaveAsFile(“d:\\ScreenShot.png”,System.Drawing.Imaging.ImageFormat.Png);
            webDriver.Quit();
            Console.ReadLine();
           
        }
    }
}
Using Anonymous Method:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
//add this name space to access WebDriverWait
using OpenQA.Selenium.Support.UI;
namespace AutomationUsingSelenium
{
    class Program
    {
        static void Main(string[] args)
        {
            var capabilitiesInternet = new OpenQA.Selenium.Remote.DesiredCapabilities();
            capabilitiesInternet.SetCapability(“ignoreProtectedModeSettings”, true);
          
            IWebDriver webDriver = new InternetExplorerDriver(capabilitiesInternet);
            webDriver.Navigate().GoToUrl(http://www.google.com”);
            IWebElement query = webDriver.FindElement(By.Name(“q”));
            query.SendKeys(http://seleniumdotnet.blogspot.com”);
            OpenQA.Selenium.Support.UI.WebDriverWait wait = new OpenQA.Selenium.Support.UI.WebDriverWait(webDriver, TimeSpan.FromSeconds(10));
            Console.WriteLine(“Page title is: “+webDriver.Title);
            //using Anonymous method
             wait.Until(delegate(IWebDriver d) { return d.FindElement(By.Id(“ab_name”)); });
            
            Screenshot screenshot = ((ITakesScreenshot) webDriver).GetScreenshot();
            screenshot.SaveAsFile(“d:\\ScreenShot.png”,System.Drawing.Imaging.ImageFormat.Png);
            webDriver.Quit();
            Console.ReadLine();
           
        }
    }
}

2 thoughts on “Wait for an element to load in selenium using C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s