By
The By class is usually used as a factory for all sorts of selenium Selectors. Each type of the selector supported by Selenium can be created by a specific static method in this class. SWE is also following this concept by providing a custom version of the By class that can create all Selenium selector types but it also can create new types of selectors that are provided in this library.
By alias
The easiest way of creating selectors in Selenium WebDriver is by using By static class members. Since in C# static class cannot be inherited, another version of this class has been created that exposes new types of selectors apart from the ones provided by Selenium WebDriver.
The most convenient way of using this class is by specifying the alias.
using By = Selenium.WebDriver.Extensions.By;The instances of the selector types added by this library can also be created by simply invoking their constructors.
var jQuerySelector = new JQuerySelector("div.myClass:visible");
var sizzleSelector = new SizzleSelector("div.myClass:visible");By.JQuerySelector
Gets a mechanism to find elements matching jQuery selector.
var element = driver.FindElement(By.JQuerySelector(":text"));Gets a mechanism to find elements matching jQuery selector with the nested context. This will invoke the nested context selector first, and on the results of this selector will perform the new query further narrowing the results.
var element = driver.FindElement(By.JQuerySelector(":text", By.JQuerySelector("div.myClass")));Gets a mechanism to find elements matching jQuery selector and overriding the jQuery variable name. This can be useful in cases where the variable name conflicts with other libraries.
var element = driver.FindElement(By.JQuerySelector(":text", variable: "myJQuery"));By.SizzleSelector
Gets a mechanism to find elements matching Sizzle selector.
var element = driver.FindElement(By.SizzleSelector(":text"));Gets a mechanism to find elements matching Sizzle selector with the nested context. This will invoke the nested context selector first, and on the results of this selector will perform the new query further narrowing the results.
var element = driver.FindElement(By.SizzleSelector(":text", By.SizzleSelector("div.myClass")));By.ClassName
Gets a mechanism to find elements by their CSS class.
var element = driver.FindElement(By.ClassName("myClass"));By.CssSelector
Gets a mechanism to find elements by their cascading style sheet (CSS) selector.
var element = driver.FindElement(By.CssSelector("div.myClass"));By.Id
Gets a mechanism to find elements by their ID.
var element = driver.FindElement(By.Id("myId"));By.Name
Gets a mechanism to find elements by their name.
var element = driver.FindElement(By.Name("myName"));By.TagName
Gets a mechanism to find elements by their tag name.
var element = driver.FindElement(By.TagName("div"));By.LinkText
Gets a mechanism to find elements by their link text.
var element = driver.FindElement(By.LinkText("text"));By.PartialLinkText
Gets a mechanism to find elements by a partial match on their link text.
var element = driver.FindElement(By.PartialLinkText("text"));By.XPath
Gets a mechanism to find elements by an XPath query. When searching within a IWebElement using xpath be aware that IWebDriver follows standard conventions: a search prefixed with / will search the entire document, not just the children of this current node. Use ./ to limit your search to the children of this IWebElement.
var element = driver.FindElement(By.XPath("//body/div"));Last updated
Was this helpful?