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
findElementMethodFunc<ISearchContext, IWebElement>A function that takes an object implementing ISearchContext and returns the found IWebElement.
findElementsMethodFunc<ISearchContext, ReadOnlyCollection<IWebElement>>A function that takes an object implementing ISearchContext and returns a ReadOnlyCollection<T> of the foundIWebElements. IWebElements/>.
By(string, string)
Initializes a new instance of the By class using the specified mechanism and criteria for finding elements.
protected By(string mechanism, string criteria)
Parameters
mechanismstringThe mechanism to use in finding elements.
criteriastringThe criteria to use in finding elements.
Remarks
Customizing nothing else, instances using this constructor will attempt to find elements 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, or throws NoSuchElementException if no element is found.
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
classNameToFindstringThe 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".
Exceptions
- ArgumentNullException
If
classNameToFindis null.
CssSelector(string)
Gets a mechanism to find elements by their cascading style sheet (CSS) selector.
public static By CssSelector(string cssSelectorToFind)
Parameters
cssSelectorToFindstringThe CSS selector to find.
Returns
Exceptions
- ArgumentNullException
If
cssSelectorToFindis null.
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
contextISearchContextAn ISearchContext object to use to search for the elements.
Returns
- IWebElement
The first matching IWebElement on the current context.
Exceptions
- NoSuchElementException
If no element matches the criteria.
FindElements(ISearchContext)
Finds all elements matching the criteria.
public virtual ReadOnlyCollection<IWebElement> FindElements(ISearchContext context)
Parameters
contextISearchContextAn 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
idToFindstringThe ID to find.
Returns
Exceptions
- ArgumentNullException
If
idToFindis null.
LinkText(string)
Gets a mechanism to find elements by their link text.
public static By LinkText(string linkTextToFind)
Parameters
linkTextToFindstringThe link text to find.
Returns
Exceptions
- ArgumentNullException
If
linkTextToFindis null.
Name(string)
Gets a mechanism to find elements by their name.
public static By Name(string nameToFind)
Parameters
nameToFindstringThe name to find.
Returns
Exceptions
- ArgumentNullException
If
nameToFindis null.
PartialLinkText(string)
Gets a mechanism to find elements by a partial match on their link text.
public static By PartialLinkText(string partialLinkTextToFind)
Parameters
partialLinkTextToFindstringThe partial link text to find.
Returns
Exceptions
- ArgumentNullException
If
partialLinkTextToFindis null.
TagName(string)
Gets a mechanism to find elements by their tag name.
public static By TagName(string tagNameToFind)
Parameters
tagNameToFindstringThe tag name to find.
Returns
Exceptions
- ArgumentNullException
If
tagNameToFindis null.
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
xpathToFindstringThe XPath query to use.
Returns
Exceptions
- ArgumentNullException
If
xpathToFindis null.
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)