public class HtmlElement extends Element
The HtmlQuery.findFirst()
method returns an HtmlElement
.
See HtmlQuery
how to find elements and Browser
how to directly
find and interact with elements.
Examples:
HtmlElement saveButton = browser.click("Save");
directly searches
for the button labeled Save and returns the HtmlElement
HtmlElement inputField = browser.type("Username", "Jack");
directly searches
for the input field labeled Username, enters the specified text and returns
the HtmlElement
HtmlElement myElement = browser.query().isTitle().isClickable().has("today").findFirst();
directly searches
for clickable titles labeled today and returns the HtmlElement
Browser
,
HtmlQuery
,
HtmlElements
Constructor and Description |
---|
HtmlElement(Browser browser,
org.openqa.selenium.remote.RemoteWebElement seleniumElement)
Creates a new
HtmlElement instance and caches most important data. |
Modifier and Type | Method and Description |
---|---|
Query |
above()
Creates an
HtmlQuery to find elements above this one. |
Query |
below()
Creates an
HtmlQuery to find elements below this one. |
HtmlElement |
choose(java.lang.String optionQuery)
Chooses option/item from select field
|
HtmlElement |
chooseDate(org.joda.time.DateTime dateTime)
Selects the specified date.
|
HtmlElement |
clear()
Clears entered text in element.
|
HtmlElement |
click()
Clicks on element.
|
HtmlElement |
doubleClick()
Doubleclicks on element.
|
HtmlElement |
dragTo(HtmlElement targetElement)
Drags an element with the mouse on another element.
|
boolean |
equals(HtmlElement otherElement)
Checks if element is equal to other element
|
org.joda.time.DateTime |
extractDate()
Extracts date from this element.
|
java.lang.String |
getAttribute(java.lang.String name)
Gets the value for the specified attribute name.
|
java.util.ArrayList<java.lang.String> |
getAttributeNames()
Gets the names of all attributes of this element.
|
java.util.Map<java.lang.String,java.lang.String> |
getAttributes() |
Browser |
getBrowser()
Gets the associated browser instance for this element.
|
HtmlElements |
getChildren()
Gets the children of this element in the document structure.
|
java.lang.String |
getCssValue(java.lang.String cssAttribute)
Gets value of a CSS attribute.
|
HtmlElement |
getFrameElement()
Gets frame element if this element is located in a frame or iframe.
|
java.lang.String |
getId()
Gets the internal id of this element.
|
Location |
getLocation()
Gets the location of this element.
|
HtmlElement |
getParent()
Gets the parent of this element in the document structure.
|
HtmlElements |
getReferenceElements()
Gets reference elements, which are needed for internal closeness calculations.
|
org.openqa.selenium.remote.RemoteWebElement |
getSeleniumElement()
Gets the associated Selenium element for this element.
|
HtmlElements |
getSiblings()
Gets the siblings of this element in the document structure.
|
Size |
getSize()
Gets the size of this element.
|
java.lang.String |
getTagName()
Gets the tag name of this element.
|
java.lang.String |
getText()
Gets visible text of element.
|
java.lang.String |
getTextFirstLine()
Gets first line of the inner text of this element.
|
java.lang.String |
getTextLine(int lineNumber)
Gets specified line of the inner text of this element.
|
java.lang.String |
getTextWithSource()
Gets inner text of element.
|
java.util.ArrayList<com.abmash.REMOVE.core.htmlquery.condition.ElementCondition.ElementType> |
getTypes()
Gets element types.
|
java.lang.String |
getUniqueSelector()
Builds unique CSS selector for this element.
|
java.lang.String |
getUrl()
Extracts an URL out of this element, which are mostly used in
<a> and <img> elements. |
java.lang.String |
getWindowName()
Gets name of window which contains this element.
|
HtmlElement |
hover()
Hovers on element with the mouse.
|
boolean |
isLocatedInFrame()
Checks if element is located in a frame or iframe.
|
HtmlElement |
keyHold(java.lang.String keyName)
Holds key on element.
|
HtmlElement |
keyPress(java.lang.String keyName)
Presses key on element.
|
HtmlElement |
keyRelease(java.lang.String keyName)
Releases key on element.
|
Query |
leftTo()
Creates an
HtmlQuery to find elements left to this one. |
HtmlElement |
rightClick()
Rightclicks on element to open the context menu.
|
Query |
rightTo()
Creates an
HtmlQuery to find elements right to this one. |
void |
setAttributeNames(java.util.ArrayList<java.lang.String> attributeNames) |
void |
setAttributes(java.util.Map<java.lang.String,java.lang.String> attributes) |
void |
setFrameElement(HtmlElement frameElement)
Sets frame element if this element is located in a frame or iframe.
|
void |
setLocation(Location location) |
void |
setReferenceElements(HtmlElement referenceElement)
Sets reference element, which is needed for internal closeness calculations.
|
void |
setReferenceElements(HtmlElements referenceElements)
Sets reference elements, which are needed for internal closeness calculations.
|
void |
setSize(Size size) |
void |
setSourceText(java.lang.String sourceText) |
void |
setTagName(java.lang.String tagName) |
void |
setText(java.lang.String text) |
void |
setTypes(java.util.ArrayList<com.abmash.REMOVE.core.htmlquery.condition.ElementCondition.ElementType> types)
Sets element types.
|
void |
setUniqueSelector(java.lang.String uniqueSelector) |
void |
setWindowName(java.lang.String windowName)
Sets name of window which contains this element.
|
HtmlElement |
submit()
Submits the form linked to this element.
|
void |
switchToElementWindow()
Switches to the window in which this element is located.
|
java.lang.String |
toString()
Outputs string representation of element.
|
void |
triggerAction(java.lang.String type,
java.lang.String param)
Performs an action on this element.
|
HtmlElement |
type(java.lang.String text)
Enters text in element.
|
HtmlElement |
unchoose(java.lang.String optionQuery)
Deselects option/item from select field
|
public HtmlElement(Browser browser, org.openqa.selenium.remote.RemoteWebElement seleniumElement)
HtmlElement
instance and caches most important data.
Cached element data is the tag name and inner text. If the page is refreshed or another URL is opened, that data is still available. But, interacting with the element like clicking or typing in will throw an Exception.
browser
- seleniumElement
- public Browser getBrowser()
public java.lang.String getId()
public org.openqa.selenium.remote.RemoteWebElement getSeleniumElement()
public HtmlElement click()
Note that if the page reloads after a click, all found HtmlElement
instances may lose their validity.
Example:
HtmlElement button = browser.query().isClickable().has("submit").findFirst();
button.click();
HtmlElement
public HtmlElement doubleClick()
Note that if the page reloads after a click, all found HtmlElement
instances may lose their validity.
Example:
HtmlElement file = browser.query().isClickable().has("file").findFirst();
file.doubleClick();
HtmlElement
public HtmlElement rightClick()
Note that if the page reloads after a click, all found HtmlElement
instances may lose their validity.
Example:
HtmlElement item = browser.query().isClickable().has("item").findFirst();
item.rightClick();
HtmlElement
public HtmlElement hover()
The "mouseover" event will be fired on this element.
Example:
HtmlElement menuLink = browser.query().isClickable().has("menu").findFirst();
menuLink.hover();
HtmlElement
public HtmlElement dragTo(HtmlElement targetElement)
Example:
HtmlElements products = browser.query().isClickable().has("item").find();
HtmlElement shoppingCart = browser.query().has("cart").findFirst();
products.dragTo(shoppingCart);
targetElement
- the element to drag the element toHtmlElement
public HtmlElement clear()
Example:
HtmlElement input = browser.query().isTypable().has("name").findFirst();
input.clear();
HtmlElement
public HtmlElement type(java.lang.String text)
Example:
HtmlElement input = browser.query().isTypable().has("name").findFirst();
input.type("John");
text
- HtmlElement
public HtmlElement choose(java.lang.String optionQuery)
Example:
HtmlElement select = browser.query().isChoosable().has("language").findFirst();
input.choose("english");
optionQuery
- text which represents the select option/itemHtmlElement
public HtmlElement unchoose(java.lang.String optionQuery)
Example:
HtmlElement select = browser.query().isSelectable().has("ingredients").findFirst();
input.unchoose("tomatoes");
optionQuery
- text which represents the select option/itemHtmlElement
public HtmlElement chooseDate(org.joda.time.DateTime dateTime)
dateTime
- HtmlElement
public HtmlElement submit()
Example:
HtmlElement input = browser.query().isTypable().has("name").findFirst();
input.submit();
Examples for forms are search inputs, contact forms or login pages. Submitting causes the form to be sent to the server and usually reloads the web page or parts of the page.
Note that if the page reloads after the submit, all found HtmlElement
instances may lose their validity.
HtmlElement
public HtmlElement keyPress(java.lang.String keyName)
Keys
for a complete list of possible key names.
Example:
HtmlElement input = browser.query().isTypable().has("name").findFirst();
input.keyPress("enter");
Note that keyPress()
and keyHold().keyRelease()
are equivalent.
keyName
- the name of the key, case-insensitiveHtmlElement
Keys
,
keyHold(String)
,
keyRelease(String)
public HtmlElement keyHold(java.lang.String keyName)
Keys
for a complete list of possible key names.
Example:
HtmlElement item = browser.query().isTypable().has("item").findFirst();
item.keyHold("ctrl");
Note that keyPress()
and keyHold().keyRelease()
are equivalent.
keyName
- the name of the key, case-insensitiveHtmlElement
Keys
,
keyPress(String)
,
keyRelease(String)
public HtmlElement keyRelease(java.lang.String keyName)
Keys
for a complete list of possible key names.
Example:
HtmlElement item = browser.query().isTypable().has("item").findFirst();
item.keyHold("ctrl");
Note that keyPress()
and keyHold().keyRelease()
are equivalent.
keyName
- the name of the key, case-insensitiveHtmlElement
Keys
,
keyPress(String)
,
keyHold(String)
public void triggerAction(java.lang.String type, java.lang.String param)
List of actions:
click
: performs a click action. param
is optional, if set to "right" it will perform a right click.dblclick
: performs a double click action. param
is not needed here.type
: performs a type action. param
is mandatory and indicates the text to type in.keypress
: performs a key press action. param
is mandatory and indicates the key code.keydown
: performs a key hold action. param
is mandatory and indicates the key code.keyup
: performs a key release action. param
is mandatory and indicates the key code.type
- the action to perform on this elementparam
- additional parameter, may be not needed, optional or mandatory. see list above for detailspublic Query below()
HtmlQuery
to find elements below this one.HtmlQuery
,
HtmlQuery.below(HtmlElement)
public Query above()
HtmlQuery
to find elements above this one.HtmlQuery
,
HtmlQuery.above(HtmlElement)
public Query leftTo()
HtmlQuery
to find elements left to this one.HtmlQuery
,
HtmlQuery.leftTo(HtmlElement)
public Query rightTo()
HtmlQuery
to find elements right to this one.HtmlQuery
,
HtmlQuery.rightTo(HtmlElement)
public void setTagName(java.lang.String tagName)
public void setText(java.lang.String text)
public void setSourceText(java.lang.String sourceText)
public void setAttributeNames(java.util.ArrayList<java.lang.String> attributeNames)
public void setAttributes(java.util.Map<java.lang.String,java.lang.String> attributes)
public void setUniqueSelector(java.lang.String uniqueSelector)
public void setLocation(Location location)
public void setSize(Size size)
public HtmlElement getParent()
Although the term parent refers to the document source code, mostly it also reflects the visual parent of this element.
public HtmlElements getSiblings()
Although the term sibling refers to the document source code, mostly it also reflects the visual siblings of this element.
public HtmlElements getChildren()
Although the term children refers to the document source code, mostly it also reflects the visual children of this element.
public java.util.ArrayList<com.abmash.REMOVE.core.htmlquery.condition.ElementCondition.ElementType> getTypes()
ElementType
ElementCondition.ElementType
public void setTypes(java.util.ArrayList<com.abmash.REMOVE.core.htmlquery.condition.ElementCondition.ElementType> types)
public java.lang.String getUniqueSelector()
public java.util.ArrayList<java.lang.String> getAttributeNames()
public java.util.Map<java.lang.String,java.lang.String> getAttributes()
public Location getLocation()
public Size getSize()
public java.lang.String getTagName()
Example: Given the following HTML source
<a href="index.html">Link text</a>
getText()
returns "a".
public java.lang.String getText()
Containing HTML source will be omitted. To get the containing HTML source too, use getTextWithSource()
instead.
Example: Given the following HTML source
<a href="path/to/index.html">Link <strong>text</strong></a>
getText()
returns "Link text".
public java.lang.String getTextWithSource()
Containing HTML source will be returned to. To omit any HTML tags use getText()
instead.
Example: Given the following HTML source
<a href="path/to/index.html">Link <strong>text</strong></a>
getTextWithSource()
returns "Link text".
public java.lang.String getTextLine(int lineNumber)
lineNumber
- public java.lang.String getTextFirstLine()
public java.lang.String getUrl()
<a>
and <img>
elements.public java.lang.String getAttribute(java.lang.String name)
Example: Given the following HTML source
<div id="content" class="container" />
getAttribute("class")
returns container
name
- the attribute namepublic java.lang.String getCssValue(java.lang.String cssAttribute)
cssAttribute
- the name of the CSS attributepublic HtmlElements getReferenceElements()
public void setReferenceElements(HtmlElements referenceElements)
referenceElements
- public void setReferenceElements(HtmlElement referenceElement)
referenceElement
- public java.lang.String getWindowName()
public void setWindowName(java.lang.String windowName)
windowName
- public HtmlElement getFrameElement()
public void setFrameElement(HtmlElement frameElement)
frameElement
- public boolean isLocatedInFrame()
public void switchToElementWindow()
public org.joda.time.DateTime extractDate() throws java.text.ParseException
Example: given the following HTML element
<div>Last updated: 5. May 2012</div>
the following code will extract the date
Date lastUpdated = browser.query().has("Last updated").findOne().extractDate();
java.text.ParseException
public boolean equals(HtmlElement otherElement)
otherElement
- HtmlElement
to compare withpublic java.lang.String toString()
toString
in class Element
Element.toString()