org.openqa.selenium
Interface WebElement

All Superinterfaces:
SearchContext
All Known Subinterfaces:
RenderedWebElement

public interface WebElement
extends SearchContext

Represents an HTML element. Generally, all interesting operations to do with interacting with a page will be performed through this interface.

All method calls will do a freshness check to ensure that the element reference is still valid. This essentially determines whether or not the element is still attached to the DOM. If this test fails, then an StaleElementReferenceException is thrown, and all future calls to this instance will fail.


Method Summary
 void clear()
          If this element is a text entry element, this will clear the value.
 void click()
          Click this element.
 WebElement findElement(By by)
          Find the first WebElement using the given method.
 java.util.List<WebElement> findElements(By by)
          Find all elements within the current context using the given mechanism.
 java.lang.String getAttribute(java.lang.String name)
          Get the value of a the given attribute of the element.
 java.lang.String getTagName()
          Get the tag name of this element.
 java.lang.String getText()
          Get the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements, without any leading or trailing whitespace.
 java.lang.String getValue()
          Get the value of the element's "value" attribute.
 boolean isEnabled()
          Is the element currently enabled or not?
 boolean isSelected()
          Determine whether or not this element is selected or not.
 void sendKeys(java.lang.CharSequence... keysToSend)
          Use this method to simulate typing into an element, which may set its value.
 void setSelected()
          Select an element.
 void submit()
          If this current element is a form, or an element within a form, then this will be submitted to the remote server.
 boolean toggle()
          If the element is a checkbox this will toggle the elements state from selected to not selected, or from not selected to selected.
 

Method Detail

click

void click()
Click this element. If this causes a new page to load, this method will block until the page has loaded. At this point, you should discard all references to this element and any further operations performed on this element will have undefined behaviour unless you know that the element and the page will still be present. If click() causes a new page to be loaded via an event or is done by sending a native event (which is a common case on Firefox, IE on Windows) then the method will *not* wait for it to be loaded and the caller should verify that a new page has been loaded.

If this element is not clickable, then this operation is a no-op since it's pretty common for someone to accidentally miss the target when clicking in Real Life


submit

void submit()
If this current element is a form, or an element within a form, then this will be submitted to the remote server. If this causes the current page to change, then this method will block until the new page is loaded.

Throws:
NoSuchElementException - If the given element is not within a form

getValue

java.lang.String getValue()
Get the value of the element's "value" attribute. If this value has been modified after the page has loaded (for example, through javascript) then this will reflect the current value of the "value" attribute.

Returns:
The value of the element's "value" attribute.
See Also:
getAttribute(String)

sendKeys

void sendKeys(java.lang.CharSequence... keysToSend)
Use this method to simulate typing into an element, which may set its value.


clear

void clear()
If this element is a text entry element, this will clear the value. Has no effect on other elements. Text entry elements are INPUT and TEXTAREA elements.


getTagName

java.lang.String getTagName()
Get the tag name of this element. Not the value of the name attribute: will return "input" for the element <input name="foo" />.

Returns:
The tag name of this element.

getAttribute

java.lang.String getAttribute(java.lang.String name)
Get the value of a the given attribute of the element. Will return the current value, even if this has been modified after the page has been loaded. More exactly, this method will return the value of the given attribute, unless that attribute is not present, in which case the value of the property with the same name is returned. If neither value is set, null is returned. The "style" attribute is converted as best can be to a text representation with a trailing semi-colon. The following are deemed to be "boolean" attributes, and will return either "true" or "false": async, autofocus, autoplay, checked, compact, complete, controls, declare, defaultchecked, defaultselected, defer, disabled, draggable, ended, formnovalidate, hidden, indeterminate, iscontenteditable, ismap, itemscope, loop, multiple, muted, nohref, noresize, noshade, novalidate, nowrap, open, paused, pubdate, readonly, required, reversed, scoped, seamless, seeking, selected, spellcheck, truespeed, willvalidate Finally, the following commonly mis-capitalized attribute/property names are evaluated as expected:

Parameters:
name - The name of the attribute.
Returns:
The attribute's current value or null if the value is not set.

toggle

boolean toggle()
If the element is a checkbox this will toggle the elements state from selected to not selected, or from not selected to selected.

Returns:
Whether the toggled element is selected (true) or not (false) after this toggle is complete

isSelected

boolean isSelected()
Determine whether or not this element is selected or not. This operation only applies to input elements such as checkboxes, options in a select and radio buttons.

Returns:
True if the element is currently selected or checked, false otherwise.

setSelected

void setSelected()
Select an element. This method will work against radio buttons, "option" elements within a "select" and checkboxes


isEnabled

boolean isEnabled()
Is the element currently enabled or not? This will generally return true for everything but disabled input elements.

Returns:
True if the element is enabled, false otherwise.

getText

java.lang.String getText()
Get the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements, without any leading or trailing whitespace.

Returns:
The innerText of this element.

findElements

java.util.List<WebElement> findElements(By by)
Find all elements within the current context using the given mechanism. When 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.

Specified by:
findElements in interface SearchContext
Parameters:
by - The locating mechanism to use
Returns:
A list of all WebElements, or an empty list if nothing matches.
See Also:
By

findElement

WebElement findElement(By by)
Find the first WebElement using the given method. See the note in findElement(By) about finding via XPath.

Specified by:
findElement in interface SearchContext
Parameters:
by - The locating mechanism
Returns:
The first matching element on the current context.
Throws:
NoSuchElementException - If no matching elements are found


Copyright © 2011. All Rights Reserved.