Class WebElement
A base class representing an HTML element on a page.
public class WebElement : IWebElement, ISearchContext, IFindsElement, IWrapsDriver, ILocatable, ITakesScreenshot
- Inheritance
-
WebElement
- Implements
- Inherited Members
Constructors
WebElement(WebDriver, string)
Initializes a new instance of the WebElement class.
public WebElement(WebDriver parentDriver, string id)
Parameters
parentDriver
WebDriverThe WebDriver instance that is driving this element.
id
stringThe ID value provided to identify the element.
Fields
ElementReferencePropertyName
The property name that represents a web element in the wire protocol.
public const string ElementReferencePropertyName = "element-6066-11e4-a52e-4f735466cecf"
Field Value
Properties
ComputedAccessibleLabel
Gets the computed accessible label of this element.
public virtual string ComputedAccessibleLabel { get; }
Property Value
ComputedAccessibleRole
Gets the computed ARIA role for this element.
public virtual string ComputedAccessibleRole { get; }
Property Value
Coordinates
Gets the coordinates identifying the location of this element using various frames of reference.
public virtual ICoordinates Coordinates { get; }
Property Value
Displayed
Gets a value indicating whether or not this element is displayed.
public virtual bool Displayed { get; }
Property Value
Remarks
The Displayed property avoids the problem of having to parse an element's "style" attribute to determine visibility of an element.
Exceptions
- StaleElementReferenceException
Thrown when the target element is no longer valid in the document DOM.
Enabled
Gets a value indicating whether or not this element is enabled.
public virtual bool Enabled { get; }
Property Value
Remarks
The Enabled property will generally return true for everything except explicitly disabled input elements.
Exceptions
- StaleElementReferenceException
Thrown when the target element is no longer valid in the document DOM.
Id
Gets the ID of the element
protected string Id { get; }
Property Value
Remarks
This property is internal to the WebDriver instance, and is not intended to be used in your code. The element's ID has no meaning outside of internal WebDriver usage, so it would be improper to scope it as public. However, both subclasses of WebElement and the parent driver hosting the element have a need to access the internal element ID. Therefore, we have two properties returning the same value, one scoped as internal, the other as protected.
Location
Gets a Point object containing the coordinates of the upper-left corner of this element relative to the upper-left corner of the page.
public virtual Point Location { get; }
Property Value
Exceptions
- StaleElementReferenceException
Thrown when the target element is no longer valid in the document DOM.
LocationOnScreenOnceScrolledIntoView
Gets the point where the element would be when scrolled into view.
public virtual Point LocationOnScreenOnceScrolledIntoView { get; }
Property Value
Selected
Gets a value indicating whether or not this element is selected.
public virtual bool Selected { get; }
Property Value
Remarks
This operation only applies to input elements such as checkboxes, options in a select element and radio buttons.
Exceptions
- StaleElementReferenceException
Thrown when the target element is no longer valid in the document DOM.
Size
Gets a Size object containing the height and width of this element.
public virtual Size Size { get; }
Property Value
Exceptions
- StaleElementReferenceException
Thrown when the target element is no longer valid in the document DOM.
TagName
Gets the tag name of this element.
public virtual string TagName { get; }
Property Value
Remarks
The TagName property returns the tag name of the element, not the value of the name attribute. For example, it will return "input" for an element specified by the HTML markup <input name="foo" />.
Exceptions
- StaleElementReferenceException
Thrown when the target element is no longer valid in the document DOM.
Text
Gets the innerText of this element, without any leading or trailing whitespace, and with other whitespace collapsed.
public virtual string Text { get; }
Property Value
Exceptions
- StaleElementReferenceException
Thrown when the target element is no longer valid in the document DOM.
WrappedDriver
Gets the IWebDriver driving this element.
public IWebDriver WrappedDriver { get; }
Property Value
Methods
Clear()
Clears the content of this element.
public virtual void Clear()
Remarks
If this element is a text entry element, the Clear() method will clear the value. It has no effect on other elements. Text entry elements are defined as elements with INPUT or TEXTAREA tags.
Exceptions
- StaleElementReferenceException
Thrown when the target element is no longer valid in the document DOM.
Click()
Clicks this element.
public virtual void Click()
Remarks
Click this element. If the click causes a new page to load, the Click() method will attempt to block until the page has loaded. After calling the Click() method, you should discard all references to this element unless you know that the element and the page will still be present. Otherwise, any further operations performed on this element will have an undefined behavior.
Exceptions
- InvalidElementStateException
Thrown when the target element is not enabled.
- ElementNotVisibleException
Thrown when the target element is not visible.
- StaleElementReferenceException
Thrown when the target element is no longer valid in the document DOM.
Equals(object)
Compares if two elements are equal
public override bool Equals(object obj)
Parameters
obj
objectObject to compare against
Returns
- bool
A boolean if it is equal or not
Execute(string, Dictionary<string, object>)
Executes a command on this element using the specified parameters.
protected virtual Response Execute(string commandToExecute, Dictionary<string, object> parameters)
Parameters
commandToExecute
stringThe DriverCommand to execute against this element.
parameters
Dictionary<string, object>A Dictionary<TKey, TValue> containing names and values of the parameters for the command.
Returns
FindElement(By)
Finds the first IWebElement using the given method.
public virtual IWebElement FindElement(By by)
Parameters
by
ByThe locating mechanism to use.
Returns
- IWebElement
The first matching IWebElement on the current context.
Exceptions
- NoSuchElementException
If no element matches the criteria.
FindElement(string, string)
Finds a child element matching the given mechanism and value.
public virtual IWebElement FindElement(string mechanism, string value)
Parameters
mechanism
stringThe mechanism by which to find the element.
value
stringThe value to use to search for the element.
Returns
- IWebElement
The first IWebElement matching the given criteria.
FindElements(By)
Finds all IWebElements within the current context using the given mechanism.
public virtual ReadOnlyCollection<IWebElement> FindElements(By by)
Parameters
by
ByThe locating mechanism to use.
Returns
- ReadOnlyCollection<IWebElement>
A ReadOnlyCollection<T> of all WebElements matching the current criteria, or an empty list if nothing matches.
FindElements(string, string)
Finds all child elements matching the given mechanism and value.
public virtual ReadOnlyCollection<IWebElement> FindElements(string mechanism, string value)
Parameters
mechanism
stringThe mechanism by which to find the elements.
value
stringThe value to use to search for the elements.
Returns
- ReadOnlyCollection<IWebElement>
A collection of all of the IWebElements matching the given criteria.
GetAttribute(string)
Gets the value of the specified attribute or property for this element.
public virtual string GetAttribute(string attributeName)
Parameters
attributeName
stringThe name of the attribute or property.
Returns
Remarks
The GetAttribute(string) method will return the current value of the attribute or property, even if the value has been modified after the page has been loaded. Note that the value of the following attributes will be returned even if there is no explicit attribute on the element:
Attribute name | Value returned if not explicitly specified | Valid element types |
---|---|---|
checked | checked | Check Box |
selected | selected | Options in Select elements |
disabled | disabled | Input and other UI elements |
Exceptions
- StaleElementReferenceException
Thrown when the target element is no longer valid in the document DOM.
GetCssValue(string)
Gets the value of a CSS property of this element.
public virtual string GetCssValue(string propertyName)
Parameters
propertyName
stringThe name of the CSS property to get the value of.
Returns
- string
The value of the specified CSS property.
Remarks
The value returned by the GetCssValue(string) method is likely to be unpredictable in a cross-browser environment. Color values should be returned as hex strings. For example, a "background-color" property set as "green" in the HTML source, will return "#008000" for its value.
Exceptions
- StaleElementReferenceException
Thrown when the target element is no longer valid in the document DOM.
GetDomAttribute(string)
Gets the value of a declared HTML attribute of this element.
public virtual string GetDomAttribute(string attributeName)
Parameters
attributeName
stringThe name of the HTML attribute to get the value of.
Returns
- string
The HTML attribute's current value. Returns a null if the value is not set or the declared attribute does not exist.
Remarks
As opposed to the GetAttribute(string) method, this method only returns attributes declared in the element's HTML markup. To access the value of an IDL property of the element, either use the GetAttribute(string) method or the GetDomProperty(string) method.
Exceptions
- StaleElementReferenceException
Thrown when the target element is no longer valid in the document DOM.
GetDomProperty(string)
Gets the value of a JavaScript property of this element.
public virtual string GetDomProperty(string propertyName)
Parameters
propertyName
stringThe name of the JavaScript property to get the value of.
Returns
- string
The JavaScript property's current value. Returns a null if the value is not set or the property does not exist.
Exceptions
- StaleElementReferenceException
Thrown when the target element is no longer valid in the document DOM.
GetHashCode()
Method to get the hash code of the element
public override int GetHashCode()
Returns
- int
Integer of the hash code for the element
GetScreenshot()
Gets a Screenshot object representing the image of this element on the screen.
public virtual Screenshot GetScreenshot()
Returns
- Screenshot
A Screenshot object containing the image.
GetShadowRoot()
Gets the representation of an element's shadow root for accessing the shadow DOM of a web component.
public virtual ISearchContext GetShadowRoot()
Returns
- ISearchContext
A shadow root representation.
Exceptions
- StaleElementReferenceException
Thrown when the target element is no longer valid in the document DOM.
- NoSuchShadowRootException
Thrown when this element does not have a shadow root.
SendKeys(string)
Simulates typing text into the element.
public virtual void SendKeys(string text)
Parameters
text
stringThe text to type into the element.
Remarks
The text to be typed may include special characters like arrow keys, backspaces, function keys, and so on. Valid special keys are defined in Keys.
Exceptions
- InvalidElementStateException
Thrown when the target element is not enabled.
- ElementNotVisibleException
Thrown when the target element is not visible.
- StaleElementReferenceException
Thrown when the target element is no longer valid in the document DOM.
- See Also
Submit()
Submits this element to the web server.
public virtual void Submit()
Remarks
If this current element is a form, or an element within a form, then this will be submitted to the web server. If this causes the current page to change, then this method will attempt to block until the new page is loaded.
Exceptions
- StaleElementReferenceException
Thrown when the target element is no longer valid in the document DOM.
ToString()
Returns a string that represents the current WebElement.
public override string ToString()
Returns
- string
A string that represents the current WebElement.