Table of Contents

Class By

Namespace
OpenQA.Selenium
Assembly
WebDriver.dll

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 string

The mechanism to use in finding elements.

criteria string

The 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

string

Description

Gets or sets the value of the description for this By class instance.

protected string Description { get; set; }

Property Value

string

FindElementMethod

Gets or sets the method used to find a single element matching specified criteria.

protected Func<ISearchContext, IWebElement> FindElementMethod { get; set; }

Property Value

Func<ISearchContext, IWebElement>

FindElementsMethod

Gets or sets the method used to find all elements matching specified criteria.

protected Func<ISearchContext, ReadOnlyCollection<IWebElement>> FindElementsMethod { get; set; }

Property Value

Func<ISearchContext, ReadOnlyCollection<IWebElement>>

Mechanism

Gets the value of the mechanism for this By class instance.

public string Mechanism { get; }

Property Value

string

Methods

ClassName(string)

Gets a mechanism to find elements by their CSS class.

public static By ClassName(string classNameToFind)

Parameters

classNameToFind string

The CSS class to find.

Returns

By

A By object the driver can use to find the elements.

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 string

The CSS selector to find.

Returns

By

A By object the driver can use to find the elements.

Equals(object)

Determines whether the specified Object is equal to the current Object.

public override bool Equals(object obj)

Parameters

obj object

The Object to compare with the current Object.

Returns

bool

true if the specified Object is equal to the current Object; otherwise, false.

FindElement(ISearchContext)

Finds the first element matching the criteria.

public virtual IWebElement FindElement(ISearchContext context)

Parameters

context ISearchContext

An 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 ISearchContext

An 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

int

A hash code for the current Object.

Id(string)

Gets a mechanism to find elements by their ID.

public static By Id(string idToFind)

Parameters

idToFind string

The ID to find.

Returns

By

A By object the driver can use to find the elements.

LinkText(string)

Gets a mechanism to find elements by their link text.

public static By LinkText(string linkTextToFind)

Parameters

linkTextToFind string

The link text to find.

Returns

By

A By object the driver can use to find the elements.

Name(string)

Gets a mechanism to find elements by their name.

public static By Name(string nameToFind)

Parameters

nameToFind string

The name to find.

Returns

By

A By object the driver can use to find the elements.

PartialLinkText(string)

Gets a mechanism to find elements by a partial match on their link text.

public static By PartialLinkText(string partialLinkTextToFind)

Parameters

partialLinkTextToFind string

The partial link text to find.

Returns

By

A By object the driver can use to find the elements.

TagName(string)

Gets a mechanism to find elements by their tag name.

public static By TagName(string tagNameToFind)

Parameters

tagNameToFind string

The tag name to find.

Returns

By

A By object the driver can use to find the elements.

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 string

The XPath query to use.

Returns

By

A By object the driver can use to find the elements.

Operators

operator ==(By, By)

Determines if two By instances are equal.

public static bool operator ==(By one, By two)

Parameters

one By

One instance to compare.

two By

The other instance to compare.

Returns

bool

true if the two instances are equal; otherwise, false.

operator !=(By, By)

Determines if two By instances are unequal.

public static bool operator !=(By one, By two)

Parameters

one By

One instance to compare.

two By

The other instance to compare.

Returns

bool

true if the two instances are not equal; otherwise, false.