The following are the list of object identifier or locators supported by selenium.
- CSS
- XPath
- ID
- Name
- Class Name
- Linktext
- Partial Linktext
- Tag Name
ID/Name is the first choose. However, if there is no ID/Name or they are dynamic you might think about CSS or XPath.
Sometimes CSS Selecor change a lot when the UI is re-design. In this case, XPath might be a good option.
There are a lot of discussion about CSS Vs. XPath. I don't think performance is a big issue. Because Selenium doesn't focus on performance testing. In most of cases page rendering take more time than identify an element. You can also check this article Css Vs. XPath. The conclusion is "For starters there is no dramatic difference in performance between XPath and CSS."
By.CssSelector("#ID")By.CssSelector(".Name") SelectElement select = new SelectElement(element);
select.SelectByText("Perth"); By.XPath("//td[text()='Profile']")
By.XPath("//a[contains(text(),'Profile')]")With relative XPath, we can locate an element directly as well irrespective of its location in the DOM.
By.XPath("//table/*/td[2][text()='Profile}']/../td[8]/div/button"); IJavaScriptExecutor js = (IJavaScriptExecutor)Browser.WebDriver;
js.ExecuteScript(
$"document.querySelector('#createModal > div > div > div.modal-body > div:nth-child(1) > span > span > span.k-input.ng-scope').innerHTML='{baseLine}'");
public static void Initialize()
{
if ( WebDriver == null )
{
TestEnvironment.Initialize();
string strBrowserType = "";
string driverPath = "";
BaseUrl = TestEnvironment.BaseUrl;
// Read from App.config
strBrowserType = ConfigurationManager.AppSettings["BrowserType"];
driverPath = ConfigurationManager.AppSettings["BrowerDriverPath"];
_browserType = strBrowserType;
if (_browserType.Equals("IE"))
{
foreach (var process in Process.GetProcessesByName("IEDriverServer"))
{
process.Kill();
}
var ieOptions = new InternetExplorerOptions();
ieOptions.IgnoreZoomLevel = true;
ieOptions.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
// To resolve IWebElement.Sendkeys() is too slow
ieOptions.RequireWindowFocus = true;
// Setting attribute EnableNativeEvents to false enable click button in IE
ieOptions.EnableNativeEvents = false;
// Setting attribute EnablePersistentHover to false enable action.MoveToElement() in IE
//IEOptions.EnablePersistentHover = false;
WebDriver = new InternetExplorerDriver(driverPath, ieOptions);
WebDriver.Manage().Window.Maximize();
TurnOnWati();
}
else if (_browserType.Equals("Chrome"))
{
foreach (var process in Process.GetProcessesByName("chromedriver"))
{
process.Kill();
}
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddArguments("start-maximized");
//WebDriver.Manage().Window.Maximize();
// Setting no-sandbox for TFS build agent
chromeOptions.AddArguments("--no-sandbox");
WebDriver = new ChromeDriver(driverPath, chromeOptions);
TurnOnWati();
}
else
{
throw new System.ArgumentException($"The browser type {_browserType} is not supported");
}
}- Wait within a time frame.
System.Threading.Thread.Sleep(10000);- Explicit waits until time out.
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(d => d.FindElement(By.Id("receiptNo")) );- Implicit waits until time out
webDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(15);Using Action Class to emulate advanced interactions, rather than using the Keyboard or Mouse directly.
var element = Browser.WebDriver.FindElement(By.XPath(xPath));
Actions actions = new Actions(Browser.WebDriver);
actions.MoveToElement(element);
actions.Perform();
element.Click();((IJavaScriptExecutor) Browser.WebDriver).ExecuteScript("window.scrollTo(0,200)");// Click the parent node make select element visible
Browser.WebDriver.FindElement(By.CssSelector("#createModal > div > div > div.modal-body > div:nth-child(1) > span > span > span.k-select")).Click();
new Actions(Browser.WebDriver)
.MoveToElement(Browser.WebDriver.FindElement(By.CssSelector("#scenarioDropdown"))).SendKeys(baseLine).Perform();Note: Can't run in C# debug mode
using System.Windows.Forms;
SendKeys.SendWait(@"C:\temp\avatar.jpg");
SendKeys.SendWait(@"{Enter}");Add user name and password in front of URL. Encrypt password in your code, so others can't see the password.
BaseUrl = $"https://{UserName}:{Password}@{BaseUrl}";Please try Actions class to first focus on the element then send required keys.
Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.click();
actions.sendKeys("SOME DATA");
actions.build().perform();OpenQA.Selenium.WebDriverException: 'Unexpected error launching Internet Explorer. Unable to use CreateProcess() API. To use CreateProcess() with Internet Explorer 8 or higher, the value of registry setting in HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\TabProcGrowth must be '0'.'