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();
}
}
}
Now you can also use lamda expression: d=>d.FindElement() instead of anonymous methods.
ReplyDeleteHi ANil,
ReplyDeleteThanks for your comments, if you see my post you can find that already i had used Lamda expression.
actually i have used both in my post!!
~jawed