This post contains the key commands for Selenium in C#

  • Initialise a new driver

IWebDriver driver = new FirefoxDriver();



  • Navigation

driver.Navigate().GoToUrl(https://google.com/);



  • Clear a field

driver.FindElement(By.XPath(“//input[@name=’sName’]”)).Clear();



  • Entering a text

driver.FindElement(By.XPath(“//input[@name=’sName’]”).SendKeys(“Testing 123”);



  • Click on an element

driver.FindElement(By.XPath(“//input[@name=’sName’]”).Click();




  • Wait until an element is found

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));

IWebElement employeeSearch = wait.Until((d) =>

{ return

d.FindElement(

By.XPath(

“/html/body[@id=’ctl00__objBodyTag’]/form[@id=’aspnetForm’]/table/tbody/tr[2]/td/table/tbody/tr/td[@id=’menuCell’]/div[@id=’menu’]/div/ul/li[@id=’user’]/ul/li[2]/a”));

});

employeeSearch.Click();




  • Switch to other windows
ReadOnlyCollection handles = driver.WindowHandles;
driver.SwitchTo().Window(handles.ElementAt(0));
 
 
  • Select an element
new SelectElement(driver.FindElement(By.XPath(“//select[@id=’test’]”))).SelectByIndex(2);
 
 
  • Sleep
System.Threading.Thread.Sleep(2000);

  • Quit
driver.Quit();