An HTML Form Element provides ready access to the form fields/controls that are associated with it. It also allows a form to easily be submitted.
<init>(org.jsoup.parser.Tag tag, [email protected] String baseUri, [email protected] Attributes attributes)Create a new, standalone form element.
| name | type | description |
|---|---|---|
| tag | org.jsoup.parser.Tag | tag of this element |
| base_uri | [email protected] String | the base URI |
| attributes | [email protected] Attributes | initial attributes |
Add a form control element to this form.
| name | type | description |
|---|---|---|
| element | org.jsoup.nodes.Element | form control to add |
Returns: this form element, for chaining
Get the list of form control elements associated with this form.
Returns: form controls associated with this element.
Get the data that this form submits. The returned list is a copy of the data, and changes to the contents of the list will not be reflected in the DOM.
Returns: a list of key vals
Prepare to submit this form. A Connection object is created with the request set up from the form values. This
Connection will inherit the settings and the cookies (etc) of the connection/session used to request this Document
(if any), as available in Document#connection()
You can then set up other options (like user-agent, timeout, cookies), then execute it.
Returns: a connection prepared from the values of this form, in the same session as the one used to request it
| IllegalArgumentException | if the form's absolute action URL cannot be determined. Make sure you pass the document's base URI when parsing. | |
spliterator()absUrl(String attributeKey)Get an absolute URL from a URL attribute that may be relative (such as an <a href> or
<img src>).
E.g.: String absUrl = linkEl.absUrl("href");
If the attribute value is already absolute (i.e. it starts with a protocol, like
http:// or https:// etc), and it successfully parses as a URL, the attribute is
returned directly. Otherwise, it is treated as a URL relative to the element's #baseUri, and made
absolute using that.
As an alternate, you can use the #attr method with the abs: prefix, e.g.:
String absUrl = linkEl.attr("abs:href");
| name | type | description |
|---|---|---|
| attribute_key | String | The attribute key |
Returns: An absolute URL if one could be made, or an empty string (not null) if the attribute was missing or could not be made successfully into a URL.
attr(String attributeKey)Get an attribute's value by its key. Case insensitive
To get an absolute URL from an attribute that may be a relative URL, prefix the key with abs:,
which is a shortcut to the #absUrl method.
String url = a.attr("abs:href");
| name | type | description |
|---|---|---|
| attribute_key | String | The attribute key. |
Returns: The attribute, or empty string if not present (to avoid nulls).
attributesSize()Get the number of attributes that this Node has.
Returns: the number of attributes
setBaseUri(String baseUri) · also: set_base_uriUpdate the base URI of this node and all of its descendants.
| name | type | description |
|---|---|---|
| base_uri | String | base URI to set |
Get a child node by its 0-based index.
| name | type | description |
|---|---|---|
| index | int | index of child node |
Returns: the child node at this index.
| IndexOutOfBoundsException | if the index is out of bounds. | |
Get this node's children. Presented as an unmodifiable list: new children can not be added, but the child nodes themselves can be manipulated.
Returns: list of children. If no children, returns an empty list.
Returns a deep copy of this node's children. Changes made to these nodes will not be reflected in the original nodes
Returns: a deep copy of this node's children
Check if this node is the same instance of another (object identity test).
For a node value equality check, see #hasSameValue(Object)
| name | type | description |
|---|---|---|
| o | [email protected] Object | other object to compare to |
Returns: true if the content of this node is the same as the other
firstChild()Gets the first child node of this node, or null if there is none. This could be any Node type, such as an
Element, TextNode, Comment, etc. Use Element#firstElementChild() to get the first Element child.
Returns: the first child node, or null if there are no children.
Gets the first sibling of this node. That may be this node.
Returns: the first sibling node
hasAttr(String attributeKey)Test if this Node has an attribute. Case insensitive.
| name | type | description |
|---|---|---|
| attribute_key | String | The attribute key to check. |
Returns: true if the attribute exists, false if not.
hasParent() · also: has_parentChecks if this node has a parent. Nodes won't have parents if (e.g.) they are newly created and not added as a child
to an existing node, or if they are a #shallowClone(). In such cases, #parent() will return null.
Returns: if this node has a parent.
hasSameValue([email protected] Object o)Check if this node has the same content as another node. A node is considered the same if its name, attributes and content match the other node; particularly its position in the tree does not influence its similarity.
| name | type | description |
|---|---|---|
| o | [email protected] Object | other object to compare to |
Returns: true if the content of this node is the same as the other
hashCode()Provides a hashCode for this Node, based on its object identity. Changes to the Node's content will not impact the result.
Returns: an object identity based hashcode for this Node
lastChild()Gets the last child node of this node, or null if there is none.
Returns: the last child node, or null if there are no children.
Gets the last sibling of this node. That may be this node.
Returns: the last sibling (aka the parent's last child)
nameIs(String normalName)Test if this node has the specified normalized name, in any namespace.
| name | type | description |
|---|---|---|
| normal_name | String | a normalized element name (e.g. div). |
Returns: true if the element's normal name matches exactly
nextElementSibling()Gets the next sibling Element of this node. E.g., if a div contains two ps, the
nextElementSibling of the first p is the second p.
This is similar to #nextSibling(), but specifically finds only Elements.
Returns: the next element, or null if there is no next element
nextSibling()Get this node's next sibling.
Returns: next sibling, or null if this is the last sibling
Returns a Stream of this Node and all of its descendant Nodes. The stream has document order.
Returns: a stream of all nodes.
nodeStream(Class<T> type)Returns a Stream of this and descendant nodes, containing only nodes of the specified type. The stream has document order.
Returns: a stream of nodes filtered by type.
outerHtml()Get the outer HTML of this node. For example, on a p element, may return <p>Para</p>.
Returns: outer HTML
ownerDocument()Gets the Document associated with this Node.
Returns: the Document associated with this Node, or null if there is no such Document.
parentElement()Gets this node's parent Element.
Returns: parent element; or null if this node has no parent.
parentElementIs(String normalName, String namespace)Test if this node's parent is an Element with the specified normalized name and namespace.
| name | type | description |
|---|---|---|
| normal_name | String | a normalized element name (e.g. div). |
| namespace | String | the namespace |
Returns: true if the parent element's normal name matches exactly, and that element is in the specified namespace
parentNameIs(String normalName)Test if this node's parent has the specified normalized name.
| name | type | description |
|---|---|---|
| normal_name | String | a normalized name (e.g. div). |
Returns: true if the parent element's normal name matches exactly
parentNode()Gets this node's parent node. Not overridable by extending classes, so useful if you really just need the Node type.
Returns: parent node; or null if no parent.
previousElementSibling()Gets the previous Element sibling of this node.
Returns: the previous element, or null if there is no previous element
previousSibling()Get this node's previous sibling.
Returns: the previous sibling, or @{code null} if this is the first sibling
remove()Remove (delete) this node from the DOM tree. If this node has children, they are also removed. If this node is an orphan, nothing happens.
Replace this node in the DOM with the supplied node.
| name | type | description |
|---|---|---|
| in | org.jsoup.nodes.Node | the node that will replace the existing node. |
siblingIndex()Get the list index of this node in its node sibling list. E.g. if this is the first node sibling, returns 0.
Returns: position in node sibling list
Retrieves this node's sibling nodes. Similar to node.parent.childNodes(), but does not
include this node (a node is not a sibling of itself).
Returns: node siblings. If the node has no parent, returns an empty list.
Get the source range (start and end positions) in the original input source from which this node was parsed. Position tracking must be enabled prior to parsing the content. For an Element, this will be the positions of the start tag.
Returns: the range for the start of the node, or untracked if its range was not tracked.
toString()Gets this node's outer HTML.
Returns: outer HTML.
unwrap()Removes this node from the DOM, and moves its children up into the node's parent. This has the effect of dropping the node but keeping its children.
For example, with the input html:
<div>One <span>Two <b>Three</b></span></div>
element.unwrap() on the span element will result in the html:
<div>One Two <b>Three</b></div>
"Two " TextNode being returned.
Returns: the first child of this node, after the node has been unwrapped. @{code Null} if the node had no children.
Add a class name to this element's class attribute.
| name | type | description |
|---|---|---|
| class_name | String | class name to add |
Returns: this element
Insert the specified HTML into the DOM after this element (as a following sibling).
| name | type | description |
|---|---|---|
| html | String | HTML to add after this element |
Returns: this element, for chaining
Insert the specified node into the DOM after this node (as a following sibling).
| name | type | description |
|---|---|---|
| node | org.jsoup.nodes.Node | to add after this element |
Returns: this element, for chaining
Find all elements under this element (including self, and children of children).
Returns: all elements
Add inner HTML to this element. The supplied HTML will be parsed, and each node appended to the end of the children.
| name | type | description |
|---|---|---|
| html | String | HTML to add inside this element, after the existing HTML |
Returns: this element
Insert a node to the end of this Element's children. The incoming node will be re-parented.
| name | type | description |
|---|---|---|
| child | org.jsoup.nodes.Node | node to add. |
Returns: this Element, for chaining
appendChildren(Collection<? extends org.jsoup.nodes.Node> children)Insert the given nodes to the end of this Element's children.
| name | type | description |
|---|---|---|
| children | Collection<? extends org.jsoup.nodes.Node> | nodes to add |
Returns: this Element, for chaining
Create a new element by tag name, and add it as this Element's last child.
| name | type | description |
|---|---|---|
| tag_name | String | the name of the tag (e.g. div). |
Returns: the new element, to allow you to add content to it, e.g.:
parent.appendElement("h1").attr("id", "header").text("Welcome");
appendElement(String tagName, String namespace)Create a new element by tag name and namespace, add it as this Element's last child.
| name | type | description |
|---|---|---|
| tag_name | String | the name of the tag (e.g. div). |
| namespace | String | the namespace of the tag (e.g. Parser#NamespaceHtml) |
Returns: the new element, in the specified namespace
Create and append a new TextNode to this element.
| name | type | description |
|---|---|---|
| text | String | the (un-encoded) text to add |
Returns: this element
Add this element to the supplied parent element, as its next child.
| name | type | description |
|---|---|---|
| parent | org.jsoup.nodes.Element | element to which this element will be appended |
Returns: this element, so that you can continue modifying the element
attr(String attributeKey, String attributeValue)Set an attribute value on this element. If this element already has an attribute with the key, its value is updated; otherwise, a new attribute is added.
Returns: this element
attr(String attributeKey, boolean attributeValue)Set a boolean attribute value on this element. Setting to true sets the attribute value to "" and
marks the attribute as boolean so no value is written out. Setting to false removes the attribute
with the same key if it exists.
| name | type | description |
|---|---|---|
| attribute_key | String | the attribute key |
| attribute_value | boolean | the attribute value |
Returns: this element
attribute(String key)Get an Attribute by key. Changes made via Attribute#setKey(String), Attribute#setValue(String) etc
will cascade back to this Element.
| name | type | description |
|---|---|---|
| key | String | the (case-sensitive) attribute key |
Returns: the Attribute for this key, or null if not present.
baseUri()Insert the specified HTML into the DOM before this element (as a preceding sibling).
| name | type | description |
|---|---|---|
| html | String | HTML to add before this element |
Returns: this element, for chaining
Insert the specified node into the DOM before this node (as a preceding sibling).
| name | type | description |
|---|---|---|
| node | org.jsoup.nodes.Node | to add before this element |
Returns: this Element, for chaining
isBlock() · also: is_blockTest if this element is a block-level element. (E.g. <div> == true or an inline element
<span> == false).
Returns: true if block, false if not (and thus inline)
Get a child element of this element, by its 0-based index number.
Note that an element can have both mixed Nodes and Elements as children. This method inspects a filtered list of children that are elements, and the index is based on that filtered list.
| name | type | description |
|---|---|---|
| index | int | the index number of the element to retrieve |
Returns: the child element, if it exists, otherwise throws an IndexOutOfBoundsException
childNodeSize()Get this element's child elements.
This is effectively a filter on #childNodes() to get Element nodes.
Returns: child elements. If this element has no children, returns an empty list.
childrenSize()Get the number of child nodes of this element that are elements.
This method works on the same filtered list like #child(int). Use #childNodes() and #childNodeSize() to get the unfiltered Nodes (e.g. includes TextNodes etc.)
Returns: the number of child nodes that are elements
className()Gets the literal value of this element's "class" attribute, which may include multiple class names, space
separated. (E.g. on <div class="header gray"> returns, "header gray")
Returns: The literal class attribute, or empty string if no class attribute set.
classNames()Get each of the element's class names. E.g. on element <div class="header gray">,
returns a set of two elements "header", "gray". Note that modifications to this set are not pushed to
the backing class attribute; use the #classNames(java.util.Set) method to persist them.
Returns: set of classnames, empty if no class attribute
Set the element's class attribute to the supplied class names.
| name | type | description |
|---|---|---|
| class_names | Set<String> | set of classes |
Returns: this element, for chaining
closest(String cssQuery)Find the closest element up the tree of parents that matches the specified CSS query. Will return itself, an
ancestor, or null if there is no such matching element.
| name | type | description |
|---|---|---|
| css_query | String | a Selector CSS query |
Returns: the closest ancestor element (possibly itself) that matches the provided evaluator. null if not
found.
closest(org.jsoup.select.Evaluator evaluator)Find the closest element up the tree of parents that matches the specified evaluator. Will return itself, an
ancestor, or null if there is no such matching element.
| name | type | description |
|---|---|---|
| evaluator | org.jsoup.select.Evaluator | a query evaluator |
Returns: the closest ancestor element (possibly itself) that matches the provided evaluator. null if not
found.
cssSelector()Get a CSS selector that will uniquely select this element.
If the element has an ID, returns #id; otherwise returns the parent (if any) CSS selector, followed by '>', followed by a unique selector for the element (tag.class.class:nth-child(n)).
Returns: the CSS Path that can be used to retrieve the element in a selector.
data()Get the combined data of this element. Data is e.g. the inside of a <script> tag. Note that data is NOT the
plain text of the element. Use #text() to get normalized, readable text for extraction, indexing, or
accessibility-oriented processing, and data() for the contents of scripts, comments, CSS styles, etc.
Returns: the data, or empty string if none
Get this element's child data nodes. The list is unmodifiable but the data nodes may be manipulated.
This is effectively a filter on #childNodes() to get Data nodes.
Returns: child data nodes. If this element has no data nodes, returns an empty list.
dataset()Get this element's HTML5 custom data attributes. Each attribute in the element that has a key starting with "data-" is included the dataset.
E.g., the element <div data-package="jsoup" data-language="Java" class="group">... has the dataset
package=jsoup, language=java.
This map is a filtered view of the element's attribute map. Changes to one map (add, remove, update) are reflected in the other map.
You can find elements that have data attributes using the [^data-] attribute key prefix selector.
Returns: a map of key=value custom data attributes.
elementIs(String normalName, String namespace)Test if this Element has the specified normalized name, and is in the specified namespace.
| name | type | description |
|---|---|---|
| normal_name | String | a normalized element name (e.g. div). |
| namespace | String | the namespace |
Returns: true if the element's normal name matches exactly, and is in the specified namespace
elementSiblingIndex()Get the list index of this element in its element sibling list. I.e. if this is the first element sibling, returns 0.
Returns: position in element sibling list
Remove all the element's child nodes. Any attributes are left as-is. Each child node has its parent set to
null.
Returns: this element
Get the source range (start and end positions) of the end (closing) tag for this Element. Position tracking must be enabled prior to parsing the content.
Returns: the range of the closing tag for this element, or untracked if its range was not tracked.
Just like #selectFirst(String), but if there is no match, throws an IllegalArgumentException. This
is useful if you want to simply abort processing on a failed match.
| name | type | description |
|---|---|---|
| css_query | String | a Selector CSS-like query |
Returns: the first matching element
| IllegalArgumentException | if no match is found | |
expectFirstNode(String cssQuery, Class<T> type)Just like #selectFirstNode(String, Class), but if there is no match, throws an
IllegalArgumentException. This is useful if you want to simply abort processing on a failed match.
| name | type | description |
|---|---|---|
| css_query | String | a Selector CSS-like query |
Returns: the first matching node
| IllegalArgumentException | if no match is found | |
firstElementChild()Gets the first child of this Element that is an Element, or null if there is none.
Returns: the first Element child node, or null.
Gets the first Element sibling of this element. That may be this element.
Returns: the first sibling that is an element (aka the parent's first element child)
forEach(java.util.function.Consumer<? super org.jsoup.nodes.Element> action)Perform the supplied action on this Element and each of its descendant Elements, during a depth-first traversal. Elements may be inspected, changed, added, replaced, or removed.
| name | type | description |
|---|---|---|
| action | java.util.function.Consumer<? super org.jsoup.nodes.Element> | the function to perform on the element |
getElementById(String id)Find an element by ID, including or under this element.
Note that this finds the first matching ID, starting with this element. If you search down from a different
starting point, it is possible to find a different element by ID. For unique element by ID within a Document,
use Document#getElementById(String)
| name | type | description |
|---|---|---|
| id | String | The ID to search for. |
Returns: The first matching element by ID, starting with this element, or null if none found.
Find elements that have a named attribute set. Case-insensitive.
| name | type | description |
|---|---|---|
| key | String | name of the attribute, e.g. href |
Returns: elements that have this attribute, empty if none
getElementsByAttributeStarting(String keyPrefix)Find elements that have an attribute name starting with the supplied prefix. Use data- to find elements
that have HTML5 datasets.
| name | type | description |
|---|---|---|
| key_prefix | String | name prefix of the attribute e.g. data- |
Returns: elements that have attribute names that start with the prefix, empty if none.
getElementsByAttributeValue(String key, String value)Find elements that have an attribute with the specific value. Case-insensitive.
| name | type | description |
|---|---|---|
| key | String | name of the attribute |
| value | String | value of the attribute |
Returns: elements that have this attribute with this value, empty if none
getElementsByAttributeValueContaining(String key, String match)Find elements that have attributes whose value contains the match string. Case-insensitive.
| name | type | description |
|---|---|---|
| key | String | name of the attribute |
| match | String | substring of value to search for |
Returns: elements that have attributes containing this text
getElementsByAttributeValueEnding(String key, String valueSuffix)Find elements that have attributes that end with the value suffix. Case-insensitive.
| name | type | description |
|---|---|---|
| key | String | name of the attribute |
| value_suffix | String | end of the attribute value |
Returns: elements that have attributes that end with the value suffix
getElementsByAttributeValueMatching(String key, java.util.regex.Pattern pattern)Find elements that have an attribute whose value matches the supplied regular expression.
| name | type | description |
|---|---|---|
| key | String | name of the attribute |
| pattern | java.util.regex.Pattern | compiled regular expression to match against attribute values |
Returns: elements that have attributes matching this regular expression
getElementsByAttributeValueMatching(String key, String regex)Find elements that have attributes whose values match the supplied regular expression.
| name | type | description |
|---|---|---|
| key | String | name of the attribute |
| regex | String | regular expression to match against attribute values. You can use embedded flags (such as (?i) and (?m)) to control regex options. |
Returns: elements that have attributes matching this regular expression
getElementsByAttributeValueNot(String key, String value)Find elements that either do not have this attribute, or have it with a different value. Case-insensitive.
| name | type | description |
|---|---|---|
| key | String | name of the attribute |
| value | String | value of the attribute |
Returns: elements that do not have a matching attribute
getElementsByAttributeValueStarting(String key, String valuePrefix)Find elements that have attributes that start with the value prefix. Case-insensitive.
| name | type | description |
|---|---|---|
| key | String | name of the attribute |
| value_prefix | String | start of attribute value |
Returns: elements that have attributes that start with the value prefix
Find elements that have this class, including or under this element. Case-insensitive.
Elements can have multiple classes (e.g. <div class="header round first">). This method
checks each class, so you can find the above with el.getElementsByClass("header");.
| name | type | description |
|---|---|---|
| class_name | String | the name of the class to search for. |
Returns: elements with the supplied class name, empty if none
Find elements whose sibling index is equal to the supplied index.
| name | type | description |
|---|---|---|
| index | int | 0-based index |
Returns: elements equal to index
getElementsByIndexGreaterThan(int index)Find elements whose sibling index is greater than the supplied index.
| name | type | description |
|---|---|---|
| index | int | 0-based index |
Returns: elements greater than index
Find elements whose sibling index is less than the supplied index.
| name | type | description |
|---|---|---|
| index | int | 0-based index |
Returns: elements less than index
Finds elements, including and recursively under this element, with the specified tag name.
| name | type | description |
|---|---|---|
| tag_name | String | The tag name to search for (case insensitively). |
Returns: a matching unmodifiable list of elements. Will be empty if this element and none of its children match.
getElementsContainingOwnText(String searchText)Find elements that directly contain the specified string. The search is case-insensitive. The text must appear directly in the element, not in any of its descendants.
| name | type | description |
|---|---|---|
| search_text | String | to look for in the element's own text |
Returns: elements that contain the string, case-insensitive.
getElementsContainingText(String searchText)Find elements that contain the specified string. The search is case-insensitive. The text may appear directly in the element, or in any of its descendants.
| name | type | description |
|---|---|---|
| search_text | String | to look for in the element's text |
Returns: elements that contain the string, case-insensitive.
getElementsMatchingOwnText(java.util.regex.Pattern pattern)Find elements whose own text matches the supplied regular expression.
| name | type | description |
|---|---|---|
| pattern | java.util.regex.Pattern | regular expression to match text against |
Returns: elements matching the supplied regular expression.
getElementsMatchingOwnText(String regex)Find elements whose own text matches the supplied regular expression.
| name | type | description |
|---|---|---|
| regex | String | regular expression to match text against. You can use embedded flags (such as (?i) and (?m)) to control regex options. |
Returns: elements matching the supplied regular expression.
getElementsMatchingText(java.util.regex.Pattern pattern)Find elements whose text matches the supplied regular expression.
| name | type | description |
|---|---|---|
| pattern | java.util.regex.Pattern | regular expression to match text against |
Returns: elements matching the supplied regular expression.
Find elements whose text matches the supplied regular expression.
| name | type | description |
|---|---|---|
| regex | String | regular expression to match text against. You can use embedded flags (such as (?i) and (?m)) to control regex options. |
Returns: elements matching the supplied regular expression.
hasClass(String className)Tests if this element has a class. Case-insensitive.
| name | type | description |
|---|---|---|
| class_name | String | name of class to check for |
Returns: true if it does, false if not
hasText() · also: has_textChecks if the current element or any of its child elements contain non-whitespace text.
Returns: true if the element has non-blank text content, false otherwise.
html()Retrieves the element's inner HTML. E.g. on a <div> with one empty <p>, would return
<p></p>. (Whereas #outerHtml() would return <div><p></p></div>.)
Returns: String of HTML.
html(T accum)Set this element's inner HTML. Clears the existing HTML first.
| name | type | description |
|---|---|---|
| html | String | HTML to parse and set into this element |
Returns: this element
id()Get the id attribute of this element.
Returns: The id attribute, if present, or an empty string if not.
Set the id attribute of this element.
| name | type | description |
|---|---|---|
| id | String | the ID value to use |
Returns: this Element, for chaining
insertChildren(int index, Collection<? extends org.jsoup.nodes.Node> children)Inserts the given child nodes into this element at the specified index. Current nodes will be shifted to the right. The inserted nodes will be moved from their current parent. To prevent moving, copy the nodes first.
| name | type | description |
|---|---|---|
| index | int | 0-based index to insert children at. Specify 0 to insert at the start, -1 at the
end |
| children | Collection<? extends org.jsoup.nodes.Node> | child nodes to insert |
Returns: this element, for chaining.
insertChildren(int index, org.jsoup.nodes.Node[] children)Inserts the given child nodes into this element at the specified index. Current nodes will be shifted to the right. The inserted nodes will be moved from their current parent. To prevent moving, copy the nodes first.
| name | type | description |
|---|---|---|
| index | int | 0-based index to insert children at. Specify 0 to insert at the start, -1 at the
end |
| children | org.jsoup.nodes.Node[] | child nodes to insert |
Returns: this element, for chaining.
is(String cssQuery)Checks if this element matches the given Selector CSS query. Also knows as matches() in the Web
DOM.
| name | type | description |
|---|---|---|
| css_query | String | a Selector CSS query |
Returns: if this element matches the query
Check if this element matches the given evaluator.
| name | type | description |
|---|---|---|
| evaluator | org.jsoup.select.Evaluator | an element evaluator |
Returns: if this element matches
Returns an Iterator that iterates this Element and each of its descendant Elements, in document order.
Returns: an Iterator
lastElementChild()Gets the last child of this Element that is an Element, or @{code null} if there is none.
Returns: the last Element child node, or null.
Gets the last element sibling of this element. That may be this element.
Returns: the last sibling that is an element (aka the parent's last element child)
Get each of the sibling elements that come after this element.
Returns: each of the element siblings after this element, or an empty list if there are no next sibling elements
nodeName()nodeValue()An Element's nodeValue is its whole own text.
normalName()Get the normalized name of this Element's tag. This will always be the lower-cased version of the tag, regardless
of the tag case preserving setting of the parser. For e.g., <DIV> and <div> both have a
normal name of div.
Returns: normal name
ownText()Gets the (normalized) text owned by this element only; does not get the combined text of all children.
For example, given HTML <p>Hello <b>there</b> now!</p>, p.ownText() returns "Hello now!",
whereas p.text() returns "Hello there now!".
Note that the text within the b element is not returned, as it is not a direct child of the p element.
Returns: decoded text, or empty string if none.
parent()Get this element's parent and ancestors, up to the document root.
Returns: this element's stack of parents, starting with the closest first.
Add inner HTML into this element. The supplied HTML will be parsed, and each node prepended to the start of the element's children.
| name | type | description |
|---|---|---|
| html | String | HTML to add inside this element, before the existing HTML |
Returns: this element
Add a node to the start of this element's children.
| name | type | description |
|---|---|---|
| child | org.jsoup.nodes.Node | node to add. |
Returns: this element, so that you can add more child nodes or elements.
prependChildren(Collection<? extends org.jsoup.nodes.Node> children)Insert the given nodes to the start of this Element's children.
| name | type | description |
|---|---|---|
| children | Collection<? extends org.jsoup.nodes.Node> | nodes to add |
Returns: this Element, for chaining
Create a new element by tag name, and add it as this Element's first child.
| name | type | description |
|---|---|---|
| tag_name | String | the name of the tag (e.g. div). |
Returns: the new element, to allow you to add content to it, e.g.:
parent.prependElement("h1").attr("id", "header").text("Welcome");
prependElement(String tagName, String namespace)Create a new element by tag name and namespace, and add it as this Element's first child.
| name | type | description |
|---|---|---|
| tag_name | String | the name of the tag (e.g. div). |
| namespace | String | the namespace of the tag (e.g. Parser#NamespaceHtml) |
Returns: the new element, in the specified namespace
Create and prepend a new TextNode to this element.
| name | type | description |
|---|---|---|
| text | String | the decoded text to add |
Returns: this element
Get each of the element siblings before this element.
Returns: the previous element siblings, or an empty list if there are none.
Remove a class name from this element's class attribute.
| name | type | description |
|---|---|---|
| class_name | String | class name to remove |
Returns: this element
Find elements that match the Selector CSS query, with this element as the starting context. Matched elements
may include this element, or any of its descendents.
If the query starts with a combinator (e.g. * or >), that will combine to this element.
This method is generally more powerful to use than the DOM-type getElementBy* methods, because
multiple filters can be combined, e.g.:
el.select("a[href]") - finds links (a tags with href attributes)el.select("a[href*=example.com]") - finds links pointing to example.com (loosely)el.select("* div") - finds all divs that descend from this element (and excludes this element)el.select("> div") - finds all divs that are direct children of this element (and excludes this element)See the query syntax documentation in org.jsoup.select.Selector.
Also known as querySelectorAll() in the Web DOM.
| name | type | description |
|---|---|---|
| css_query | String | a Selector CSS-like query |
Returns: an Elements list containing elements that match the query (empty if none match)
| Selector.SelectorParseException | (unchecked) on an invalid CSS query. | |
Find elements that match the supplied Evaluator. This has the same functionality as #select(String), but
may be useful if you are running the same query many times (on many documents) and want to save the overhead of
repeatedly parsing the CSS query.
| name | type | description |
|---|---|---|
| evaluator | org.jsoup.select.Evaluator | an element evaluator |
Returns: an Elements list containing elements that match the query (empty if none match)
selectFirst(String cssQuery)Find the first Element that matches the Selector CSS query, with this element as the starting context.
This is effectively the same as calling element.select(query).first(), but is more efficient as query
execution stops on the first hit.
Also known as querySelector() in the Web DOM.
| name | type | description |
|---|---|---|
| css_query | String | cssQuery a Selector CSS-like query |
Returns: the first matching element, or null if there is no match.
selectFirst(org.jsoup.select.Evaluator evaluator)Finds the first Element that matches the supplied Evaluator, with this element as the starting context, or
null if none match.
| name | type | description |
|---|---|---|
| evaluator | org.jsoup.select.Evaluator | an element evaluator |
Returns: the first matching element (walking down the tree, starting from this element), or null if none
match.
selectFirstNode(String cssQuery, Class<T> type)Find the first Node that matches the Selector CSS query, with this element as the starting context.
This is effectively the same as calling element.selectNodes(query).first(), but is more efficient as
query
execution stops on the first hit.
Also known as querySelector() in the Web DOM.
| name | type | description |
|---|---|---|
| css_query | String | cssQuery a Selector CSS-like query |
Returns: the first matching node, or null if there is no match.
selectFirstNode(org.jsoup.select.Evaluator evaluator, Class<T> type)Finds the first Node that matches the supplied Evaluator, with this element as the starting context, or
null if none match.
| name | type | description |
|---|---|---|
| evaluator | org.jsoup.select.Evaluator | an element evaluator |
Returns: the first matching node (walking down the tree, starting from this element), or null if none
match.
selectNodes(org.jsoup.select.Evaluator evaluator)Find nodes that match the supplied Evaluator, with this element as the starting context. Matched
nodes may include this element, or any of its descendents.
| name | type | description |
|---|---|---|
| evaluator | org.jsoup.select.Evaluator | an evaluator |
Returns: a list of nodes that match the query (empty if none match)
Find nodes that match the supplied Selector CSS query, with this element as the starting context. Matched
nodes may include this element, or any of its descendents.
To select leaf nodes, the query should specify the node type, e.g. ::text,
::comment, ::data, ::leafnode.
| name | type | description |
|---|---|---|
| css_query | String | a Selector CSS query |
Returns: a list of nodes that match the query (empty if none match)
selectNodes(org.jsoup.select.Evaluator evaluator, Class<T> type)Find nodes that match the supplied Evaluator, with this element as the starting context. Matched nodes may include this element, or any of its descendents.
| name | type | description |
|---|---|---|
| evaluator | org.jsoup.select.Evaluator | an evaluator |
| type | Class<T> | the type of node to collect (e.g. Element, LeafNode, TextNode etc) |
Returns: a list of nodes that match the query (empty if none match)
selectNodes(String cssQuery, Class<T> type)Find nodes that match the supplied Selector CSS query, with this element as the starting context. Matched
nodes may include this element, or any of its descendents.
To select specific node types, use ::text, ::comment, ::leafnode, etc. For example, to
select all text nodes under p elements:
Nodes<TextNode> textNodes = doc.selectNodes("p ::text", TextNode.class);
| name | type | description |
|---|---|---|
| css_query | String | a Selector CSS query |
| type | Class<T> | the type of node to collect (e.g. Element, LeafNode, TextNode etc) |
Returns: a list of nodes that match the query (empty if none match)
Selects elements from the given root that match the specified Selector CSS query, with this element as the
starting context, and returns them as a lazy Stream. Matched elements may include this element, or any of its
children.
Unlike #select(String query), which returns a complete list of all matching elements, this method returns a
Stream that processes elements lazily as they are needed. The stream operates in a "pull" model — elements
are fetched from the root as the stream is traversed. You can use standard Stream operations such as
filter, map, or findFirst to process elements on demand.
| name | type | description |
|---|---|---|
| css_query | String | a Selector CSS-like query |
Returns: a Stream containing elements that match the query (empty if none match)
| Selector.SelectorParseException | (unchecked) on an invalid CSS query. | |
selectStream(org.jsoup.select.Evaluator evaluator)Find a Stream of elements that match the supplied Evaluator.
| name | type | description |
|---|---|---|
| evaluator | org.jsoup.select.Evaluator | an element Evaluator |
Returns: a Stream containing elements that match the query (empty if none match)
Find Elements that match the supplied expression.
Note that for convenience of writing the Xpath expression, namespaces are disabled, and queries can be expressed using the element's local name only.
By default, XPath 1.0 expressions are supported. If you would to use XPath 2.0 or higher, you can provide an alternate XPathFactory implementation:
javax.xml.xpath.XPathFactory:jsoup to the implementing classname. E.g.:System.setProperty(W3CDom.XPathFactoryProperty, "net.sf.saxon.xpath.XPathFactoryImpl");
| name | type | description |
|---|---|---|
| xpath | String | XPath expression |
Returns: matching elements, or an empty list if none match.
selectXpath(String xpath, Class<T> nodeType)Find Nodes that match the supplied XPath expression.
For example, to select TextNodes under p elements:
List<TextNode> textNodes = doc.selectXpath("//body//p//text()", TextNode.class);
Note that in the jsoup DOM, Attribute objects are not Nodes. To directly select attribute values, do something like:
List<String> hrefs = doc.selectXpath("//a").eachAttr("href");
| name | type | description |
|---|---|---|
| xpath | String | XPath expression |
| node_type | Class<T> | the jsoup node type to return |
Returns: a list of matching nodes
Get sibling elements. If the element has no sibling elements, returns an empty list. An element is not a sibling of itself, so will not be included in the returned list.
Returns: sibling elements
Returns a Stream of this Element and all of its descendant Elements. The stream has document order.
Returns: a stream of this element and its descendants.
Change the Tag of this element.
| name | type | description |
|---|---|---|
| tag | org.jsoup.parser.Tag | the new tag |
Returns: this element, for chaining
tagName()Get the name of the tag for this element. E.g. div. If you are using case preserving parsing, this will return the source's original case.
Returns: the tag name
Change (rename) the tag of this element. For example, convert a <span> to a <div> with
el.tagName("div");.
| name | type | description |
|---|---|---|
| tag_name | String | new tag name for this element |
Returns: this element, for chaining
tagName(String tagName, String namespace)Change (rename) the tag of this element. For example, convert a <span> to a <div> with
el.tagName("div");.
| name | type | description |
|---|---|---|
| tag_name | String | new tag name for this element |
| namespace | String | the new namespace for this element |
Returns: this element, for chaining
text()Gets the normalized, combined text of this element and all its children. Whitespace is normalized and trimmed.
For example, given HTML <p>Hello <b>there</b> now! </p>, p.text() returns "Hello there
now!"
If you do not want normalized text, use #wholeText(). If you want just the text of this node (and not
children), use #ownText().
This method returns normalized, readable plain text for downstream uses such as data extraction,
indexing, and accessibility-oriented processing. The contents of data nodes (such as
<script> tags) are not considered text. Use #data() or #html() to retrieve
that content.
Returns: decoded, normalized text, or empty string if none.
Set the text of this element. Any existing contents (text or elements) will be cleared.
As a special case, for <script> and <style> tags, the input text will be treated as data,
not visible text.
| name | type | description |
|---|---|---|
| text | String | decoded text |
Returns: this element
Get this element's child text nodes. The list is unmodifiable but the text nodes may be manipulated.
This is effectively a filter on #childNodes() to get Text nodes.
Returns: child text nodes. If this element has no text nodes, returns an empty list.
For example, with the input HTML:<p>One <span>Two</span> Three <br> Four</p> with the p element selected:
p.text() = "One Two Three Four"p.ownText() = "One Three Four"p.children() = Elements[<span>, <br>]p.childNodes() = List<Node>["One ", <span>, " Three ", <br>, " Four"]p.textNodes() = List<TextNode>["One ", " Three ", " Four"]Toggle a class name on this element's class attribute: if present, remove it; otherwise add it.
| name | type | description |
|---|---|---|
| class_name | String | class name to toggle |
Returns: this element
traverse(org.jsoup.select.NodeVisitor nodeVisitor)val()Get the value of a form element (input, textarea, etc).
Returns: the value of the form element, or empty string if not set.
Set the value of a form element (input, textarea, etc).
| name | type | description |
|---|---|---|
| value | String | value to set |
Returns: this element (for chaining)
wholeOwnText()Get the non-normalized, decoded text of this element, not including any child elements, including any newlines and spaces present in the original source.
Returns: decoded, non-normalized text that is a direct child of this Element
wholeText()Get the decoded text of this element and its children, preserving source whitespace and newlines from text nodes.
Unlike #text(), no separators are inferred around element boundaries; <br> elements are returned
as newlines.
Returns: decoded, non-normalized text
Wrap the supplied HTML around this element.
| name | type | description |
|---|---|---|
| html | String | HTML to wrap around this element, e.g. <div class="head"></div>. Can be arbitrarily deep. |
Returns: this element, for chaining.