Class By
Provides a mechanism by which to find elements within a document.
[Serializable]
public class By
- Inheritance
-
By
- Derived
- Inherited Members
Remarks
It is possible to create your own locating mechanisms for finding documents. In order to do this,subclass this class and override the protected methods. However, it is expected that that all subclasses rely on the basic finding mechanisms provided through static methods of this class. An example of this can be found in OpenQA.Support.ByIdOrName
Constructors
By()
Initializes a new instance of the By class.
protected By()
By(Func<ISearchContext, IWebElement>, Func<ISearchContext, ReadOnlyCollection<IWebElement>>)
Initializes a new instance of the By class using the given functions to find elements.
protected By(Func<ISearchContext, IWebElement> findElementMethod, Func<ISearchContext, ReadOnlyCollection<IWebElement>> findElementsMethod)
Parameters
findElementMethod
Func<ISearchContext, IWebElement>A function that takes an object implementing ISearchContext and returns the found IWebElement.
findElementsMethod
Func<ISearchContext, ReadOnlyCollection<IWebElement>>A function that takes an object implementing ISearchContext and returns a ReadOnlyCollection<T> of the foundIWebElements. IWebElements/>.
By(string, string)
Intializes a new instance of the By class using the specified mechanism and critieria for finding elements.
protected By(string mechanism, string criteria)
Parameters
mechanism
stringThe mechanism to use in finding elements.
criteria
stringThe criteria to use in finding elements.
Remarks
Customizing nothing else, instances using this constructor will attempt to find elemsnts using the FindElement(string, string) method, taking string arguments.
Properties
Criteria
Gets the value of the criteria for this By class instance.
public string Criteria { get; }
Property Value
Description
Gets or sets the value of the description for this By class instance.
protected string Description { get; set; }
Property Value
FindElementMethod
Gets or sets the method used to find a single element matching specified criteria.
protected Func<ISearchContext, IWebElement> FindElementMethod { get; set; }
Property Value
FindElementsMethod
Gets or sets the method used to find all elements matching specified criteria.
protected Func<ISearchContext, ReadOnlyCollection<IWebElement>> FindElementsMethod { get; set; }
Property Value
Mechanism
Gets the value of the mechanism for this By class instance.
public string Mechanism { get; }
Property Value
Methods
ClassName(string)
Gets a mechanism to find elements by their CSS class.
public static By ClassName(string classNameToFind)
Parameters
classNameToFind
stringThe CSS class to find.
Returns
Remarks
If an element has many classes then this will match against each of them. For example if the value is "one two onone", then the following values for the className parameter will match: "one" and "two".
CssSelector(string)
Gets a mechanism to find elements by their cascading style sheet (CSS) selector.
public static By CssSelector(string cssSelectorToFind)
Parameters
cssSelectorToFind
stringThe CSS selector to find.
Returns
Equals(object)
public override bool Equals(object obj)
Parameters
Returns
FindElement(ISearchContext)
Finds the first element matching the criteria.
public virtual IWebElement FindElement(ISearchContext context)
Parameters
context
ISearchContextAn ISearchContext object to use to search for the elements.
Returns
- IWebElement
The first matching IWebElement on the current context.
FindElements(ISearchContext)
Finds all elements matching the criteria.
public virtual ReadOnlyCollection<IWebElement> FindElements(ISearchContext context)
Parameters
context
ISearchContextAn ISearchContext object to use to search for the elements.
Returns
- ReadOnlyCollection<IWebElement>
A ReadOnlyCollection<T> of all WebElements matching the current criteria, or an empty list if nothing matches.
GetHashCode()
Serves as a hash function for a particular type.
public override int GetHashCode()
Returns
Id(string)
Gets a mechanism to find elements by their ID.
public static By Id(string idToFind)
Parameters
idToFind
stringThe ID to find.
Returns
LinkText(string)
Gets a mechanism to find elements by their link text.
public static By LinkText(string linkTextToFind)
Parameters
linkTextToFind
stringThe link text to find.
Returns
Name(string)
Gets a mechanism to find elements by their name.
public static By Name(string nameToFind)
Parameters
nameToFind
stringThe name to find.
Returns
PartialLinkText(string)
Gets a mechanism to find elements by a partial match on their link text.
public static By PartialLinkText(string partialLinkTextToFind)
Parameters
partialLinkTextToFind
stringThe partial link text to find.
Returns
TagName(string)
Gets a mechanism to find elements by their tag name.
public static By TagName(string tagNameToFind)
Parameters
tagNameToFind
stringThe tag name to find.
Returns
ToString()
Gets a string representation of the finder.
public override string ToString()
Returns
- string
The string displaying the finder content.
XPath(string)
Gets a mechanism to find elements by an XPath query. When searching within a WebElement using xpath be aware that WebDriver 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 WebElement.
public static By XPath(string xpathToFind)
Parameters
xpathToFind
stringThe XPath query to use.
Returns
Operators
operator ==(By, By)
Determines if two By instances are equal.
public static bool operator ==(By one, By two)
Parameters
Returns
operator !=(By, By)
Determines if two By instances are unequal.
public static bool operator !=(By one, By two)