jruby/docs BETA
org.jsoup.nodes 17
C Attribute
C Attributes
C CDataNode
C Comment
C DataNode
C Document
C DocumentType
C Element
C Entities
C FormElement
C LeafNode
C Node
C NodeIterator
C PseudoTextElement
C Range
C TextNode
C XmlDeclaration
FormElement — members 6+
C new(tag, base_uri, attributes)
M add_element(element) FormElement
M clone() FormElement
M elements() Elements
M form_data() KeyVal>
M submit() Connection
from Iterable
M spliterator() Spliterator<T>
from Node
M abs_url(attribute_key) String
M attr(attribute_key) String
M attributes_size() int
C base_uri=(base_uri)
M child_node(index) Node
M child_nodes() Node>
M child_nodes_copy() Node>
M equals(o) boolean
M first_child() Nullable Node
M first_sibling() Node
M has_attr(attribute_key) boolean
M has_parent?() boolean
M has_same_value(o) boolean
M hash_code() int
M last_child() Nullable Node
M last_sibling() Node
M name_is(normal_name) boolean
M next_element_sibling() Nullable Element
M next_sibling() Nullable Node
M node_stream() Node>
M node_stream(type) Stream<T>
M outer_html() String
M owner_document() Nullable Document
M parent_element() Nullable Element
M parent_element_is(normal_name, namespace) boolean
M parent_name_is(normal_name) boolean
M parent_node() Nullable Node
M previous_element_sibling() Nullable Element
M previous_sibling() Nullable Node
C remove()
C replace_with(in)
M sibling_index() int
M sibling_nodes() Node>
M source_range() Range
M to_string() String
M unwrap() Nullable Node
from Element
M add_class(class_name) Element
M after(html) Element
M after(node) Element
M all_elements() Elements
M append(html) Element
M append_child(child) Element
M append_children(children) Element
M append_element(tag_name) Element
M append_element(tag_name, namespace) Element
M append_text(text) Element
M append_to(parent) Element
M attr(attribute_key, attribute_value) Element
M attr(attribute_key, attribute_value) Element
M attribute(key) Nullable Attribute
M attributes() Attributes
M base_uri() String
M before(html) Element
M before(node) Element
M block?() boolean
M child(index) Element
M child_node_size() int
M children() Elements
M children_size() int
M class_name() String
M class_names() Set<String>
M class_names(class_names) Element
M clear_attributes() Element
M closest(css_query) Nullable Element
M closest(evaluator) Nullable Element
M css_selector() String
M data() String
M data_nodes() DataNode>
M dataset() Map<String,String>
M element_is(normal_name, namespace) boolean
M element_sibling_index() int
M empty() Element
M end_source_range() Range
M expect_first(css_query) Element
M expect_first_node(css_query, type) T
M filter(node_filter) Element
M first_element_child() Nullable Element
M first_element_sibling() Element
C for_each(action)
M for_each_node(action) Element
M get_element_by_id(id) Nullable Element
M get_elements_by_attribute(key) Elements
M get_elements_by_attribute_starting(key_prefix) Elements
M get_elements_by_attribute_value(key, value) Elements
M get_elements_by_attribute_value_containing(key, match) Elements
M get_elements_by_attribute_value_ending(key, value_suffix) Elements
M get_elements_by_attribute_value_matching(key, pattern) Elements
M get_elements_by_attribute_value_matching(key, regex) Elements
M get_elements_by_attribute_value_not(key, value) Elements
M get_elements_by_attribute_value_starting(key, value_prefix) Elements
M get_elements_by_class(class_name) Elements
M get_elements_by_index_equals(index) Elements
M get_elements_by_index_greater_than(index) Elements
M get_elements_by_index_less_than(index) Elements
M get_elements_by_tag(tag_name) Elements
M get_elements_containing_own_text(search_text) Elements
M get_elements_containing_text(search_text) Elements
M get_elements_matching_own_text(pattern) Elements
M get_elements_matching_own_text(regex) Elements
M get_elements_matching_text(pattern) Elements
M get_elements_matching_text(regex) Elements
M has_class(class_name) boolean
M has_text?() boolean
M html() String
M html(accum) T
M html(html) Element
M id() String
M id(id) Element
M insert_children(index, children) Element
M insert_children(index, children) Element
M is(css_query) boolean
M is(evaluator) boolean
M iterator() Element>
M last_element_child() Nullable Element
M last_element_sibling() Element
M next_element_siblings() Elements
M node_name() String
M node_value() String
M normal_name() String
M own_text() String
M parent() Nullable Element
M parents() Elements
M prepend(html) Element
M prepend_child(child) Element
M prepend_children(children) Element
M prepend_element(tag_name) Element
M prepend_element(tag_name, namespace) Element
M prepend_text(text) Element
M previous_element_siblings() Elements
M remove_attr(attribute_key) Element
M remove_class(class_name) Element
M root() Element
M select(css_query) Elements
M select(evaluator) Elements
M select_first(css_query) Nullable Element
M select_first(evaluator) Nullable Element
M select_first_node(css_query, type) Nullable T
M select_first_node(evaluator, type) Nullable T
M select_nodes(evaluator) Node>
M select_nodes(css_query) Node>
M select_nodes(evaluator, type) Nodes<T>
M select_nodes(css_query, type) Nodes<T>
M select_stream(css_query) Element>
M select_stream(evaluator) Element>
M select_xpath(xpath) Elements
M select_xpath(xpath, node_type) List<T>
M shallow_clone() Element
M sibling_elements() Elements
M stream() Element>
M tag() Tag
M tag(tag) Element
M tag_name() String
M tag_name(tag_name) Element
M tag_name(tag_name, namespace) Element
M text() String
M text(text) Element
M text_nodes() TextNode>
M toggle_class(class_name) Element
M traverse(node_visitor) Element
M val() String
M val(value) Element
M whole_own_text() String
M whole_text() String
M wrap(html) Element

org.jsoup.nodes.FormElement

class extends Element 6 members

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.

Constructors

new

new ( Tag tag, java.lang.@Nullable String base_uri, org.jsoup.nodes.@Nullable Attributes attributes )
Java: <init>(org.jsoup.parser.Tag tag, [email protected] String baseUri, [email protected] Attributes attributes)

Create a new, standalone form element.

nametypedescription
tagorg.jsoup.parser.Tagtag of this element
base_uri[email protected] Stringthe base URI
attributes[email protected] Attributesinitial attributes

Instance Methods

add_element

FormElement add_element ( Element element )
Java: addElement(org.jsoup.nodes.Element element)

Add a form control element to this form.

nametypedescription
elementorg.jsoup.nodes.Elementform control to add

Returns: this form element, for chaining

clone

FormElement clone ( )
Java: clone()

elements

Elements elements ( )
Java: elements()

Get the list of form control elements associated with this form.

Returns: form controls associated with this element.

form_data

List<Connection.KeyVal> form_data ( )
Java: formData()

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

submit

Connection submit ( )
Java: submit()

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

Throws

IllegalArgumentException if the form's absolute action URL cannot be determined. Make sure you pass the document's base URI when parsing.

Inherited

from Iterable

spliterator

Spliterator<T> spliterator ( )
Java: spliterator()

from Node

abs_url

String abs_url ( String attribute_key )
Java: 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");

nametypedescription
attribute_keyStringThe 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 attr ( String attribute_key )
Java: 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.

E.g.:
String url = a.attr("abs:href");

nametypedescription
attribute_keyStringThe attribute key.

Returns: The attribute, or empty string if not present (to avoid nulls).

attributes_size

int attributes_size ( )
Java: attributesSize()

Get the number of attributes that this Node has.

Returns: the number of attributes

base_uri=

base_uri= ( String base_uri )
Java: setBaseUri(String baseUri) · also: set_base_uri

Update the base URI of this node and all of its descendants.

nametypedescription
base_uriStringbase URI to set

child_node

Node child_node ( int index )
Java: childNode(int index)

Get a child node by its 0-based index.

nametypedescription
indexintindex of child node

Returns: the child node at this index.

Throws

IndexOutOfBoundsException if the index is out of bounds.

child_nodes

List<Node> child_nodes ( )
Java: childNodes()

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.

child_nodes_copy

List<Node> child_nodes_copy ( )
Java: childNodesCopy()

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

equals

boolean equals ( java.lang.@Nullable Object o )
Java: equals([email protected] Object o)

Check if this node is the same instance of another (object identity test).

For a node value equality check, see #hasSameValue(Object)

nametypedescription
o[email protected] Objectother object to compare to

Returns: true if the content of this node is the same as the other

first_child

org.jsoup.nodes.@Nullable Node first_child ( )
Java: 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.

first_sibling

Node first_sibling ( )
Java: firstSibling()

Gets the first sibling of this node. That may be this node.

Returns: the first sibling node

has_attr

boolean has_attr ( String attribute_key )
Java: hasAttr(String attributeKey)

Test if this Node has an attribute. Case insensitive.

nametypedescription
attribute_keyStringThe attribute key to check.

Returns: true if the attribute exists, false if not.

has_parent?

boolean has_parent? ( )
Java: hasParent() · also: has_parent

Checks 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.

has_same_value

boolean has_same_value ( java.lang.@Nullable Object o )
Java: 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.

nametypedescription
o[email protected] Objectother object to compare to

Returns: true if the content of this node is the same as the other

hash_code

int hash_code ( )
Java: 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

last_child

org.jsoup.nodes.@Nullable Node last_child ( )
Java: 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.

last_sibling

Node last_sibling ( )
Java: lastSibling()

Gets the last sibling of this node. That may be this node.

Returns: the last sibling (aka the parent's last child)

name_is

boolean name_is ( String normal_name )
Java: nameIs(String normalName)

Test if this node has the specified normalized name, in any namespace.

nametypedescription
normal_nameStringa normalized element name (e.g. div).

Returns: true if the element's normal name matches exactly

next_element_sibling

org.jsoup.nodes.@Nullable Element next_element_sibling ( )
Java: 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

next_sibling

org.jsoup.nodes.@Nullable Node next_sibling ( )
Java: nextSibling()

Get this node's next sibling.

Returns: next sibling, or null if this is the last sibling

node_stream

Stream<Node> node_stream ( )
Java: nodeStream()

Returns a Stream of this Node and all of its descendant Nodes. The stream has document order.

Returns: a stream of all nodes.

node_stream

Stream<T> node_stream ( Class<T> type )
Java: 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.

outer_html

String outer_html ( )
Java: outerHtml()

Get the outer HTML of this node. For example, on a p element, may return <p>Para</p>.

Returns: outer HTML

owner_document

org.jsoup.nodes.@Nullable Document owner_document ( )
Java: ownerDocument()

Gets the Document associated with this Node.

Returns: the Document associated with this Node, or null if there is no such Document.

parent_element

org.jsoup.nodes.@Nullable Element parent_element ( )
Java: parentElement()

Gets this node's parent Element.

Returns: parent element; or null if this node has no parent.

parent_element_is

boolean parent_element_is ( String normal_name, String namespace )
Java: parentElementIs(String normalName, String namespace)

Test if this node's parent is an Element with the specified normalized name and namespace.

nametypedescription
normal_nameStringa normalized element name (e.g. div).
namespaceStringthe namespace

Returns: true if the parent element's normal name matches exactly, and that element is in the specified namespace

parent_name_is

boolean parent_name_is ( String normal_name )
Java: parentNameIs(String normalName)

Test if this node's parent has the specified normalized name.

nametypedescription
normal_nameStringa normalized name (e.g. div).

Returns: true if the parent element's normal name matches exactly

parent_node

org.jsoup.nodes.@Nullable Node parent_node ( )
Java: 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.

previous_element_sibling

org.jsoup.nodes.@Nullable Element previous_element_sibling ( )
Java: previousElementSibling()

Gets the previous Element sibling of this node.

Returns: the previous element, or null if there is no previous element

previous_sibling

org.jsoup.nodes.@Nullable Node previous_sibling ( )
Java: previousSibling()

Get this node's previous sibling.

Returns: the previous sibling, or @{code null} if this is the first sibling

remove

remove ( )
Java: 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_with

replace_with ( Node in )
Java: replaceWith(org.jsoup.nodes.Node in)

Replace this node in the DOM with the supplied node.

nametypedescription
inorg.jsoup.nodes.Nodethe node that will replace the existing node.

sibling_index

int sibling_index ( )
Java: 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

sibling_nodes

List<Node> sibling_nodes ( )
Java: siblingNodes()

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.

source_range

Range source_range ( )
Java: sourceRange()

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.

to_string

String to_string ( )
Java: toString()

Gets this node's outer HTML.

Returns: outer HTML.

unwrap

org.jsoup.nodes.@Nullable Node unwrap ( )
Java: 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>

Calling element.unwrap() on the span element will result in the html:

<div>One Two <b>Three</b></div>

and the "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.

from Element

add_class

Element add_class ( String class_name )
Java: addClass(String className)

Add a class name to this element's class attribute.

nametypedescription
class_nameStringclass name to add

Returns: this element

after

Element after ( String html )
Java: after(String html)

Insert the specified HTML into the DOM after this element (as a following sibling).

nametypedescription
htmlStringHTML to add after this element

Returns: this element, for chaining

after

Element after ( Node node )
Java: after(org.jsoup.nodes.Node node)

Insert the specified node into the DOM after this node (as a following sibling).

nametypedescription
nodeorg.jsoup.nodes.Nodeto add after this element

Returns: this element, for chaining

all_elements

Elements all_elements ( )
Java: getAllElements() · also: get_all_elements

Find all elements under this element (including self, and children of children).

Returns: all elements

append

Element append ( String html )
Java: append(String html)

Add inner HTML to this element. The supplied HTML will be parsed, and each node appended to the end of the children.

nametypedescription
htmlStringHTML to add inside this element, after the existing HTML

Returns: this element

append_child

Element append_child ( Node child )
Java: appendChild(org.jsoup.nodes.Node child)

Insert a node to the end of this Element's children. The incoming node will be re-parented.

nametypedescription
childorg.jsoup.nodes.Nodenode to add.

Returns: this Element, for chaining

append_children

Element append_children ( Collection<? extends Node> children )
Java: appendChildren(Collection<? extends org.jsoup.nodes.Node> children)

Insert the given nodes to the end of this Element's children.

nametypedescription
childrenCollection<? extends org.jsoup.nodes.Node>nodes to add

Returns: this Element, for chaining

append_element

Element append_element ( String tag_name )
Java: appendElement(String tagName)

Create a new element by tag name, and add it as this Element's last child.

nametypedescription
tag_nameStringthe 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");

append_element

Element append_element ( String tag_name, String namespace )
Java: appendElement(String tagName, String namespace)

Create a new element by tag name and namespace, add it as this Element's last child.

nametypedescription
tag_nameStringthe name of the tag (e.g. div).
namespaceStringthe namespace of the tag (e.g. Parser#NamespaceHtml)

Returns: the new element, in the specified namespace

append_text

Element append_text ( String text )
Java: appendText(String text)

Create and append a new TextNode to this element.

nametypedescription
textStringthe (un-encoded) text to add

Returns: this element

append_to

Element append_to ( Element parent )
Java: appendTo(org.jsoup.nodes.Element parent)

Add this element to the supplied parent element, as its next child.

nametypedescription
parentorg.jsoup.nodes.Elementelement to which this element will be appended

Returns: this element, so that you can continue modifying the element

attr

Element attr ( String attribute_key, String attribute_value )
Java: 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

Element attr ( String attribute_key, boolean attribute_value )
Java: 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.

nametypedescription
attribute_keyStringthe attribute key
attribute_valuebooleanthe attribute value

Returns: this element

attribute

org.jsoup.nodes.@Nullable Attribute attribute ( String key )
Java: attribute(String key)

Get an Attribute by key. Changes made via Attribute#setKey(String), Attribute#setValue(String) etc will cascade back to this Element.

nametypedescription
keyStringthe (case-sensitive) attribute key

Returns: the Attribute for this key, or null if not present.

attributes

Attributes attributes ( )
Java: attributes()

base_uri

String base_uri ( )
Java: baseUri()

before

Element before ( String html )
Java: before(String html)

Insert the specified HTML into the DOM before this element (as a preceding sibling).

nametypedescription
htmlStringHTML to add before this element

Returns: this element, for chaining

before

Element before ( Node node )
Java: before(org.jsoup.nodes.Node node)

Insert the specified node into the DOM before this node (as a preceding sibling).

nametypedescription
nodeorg.jsoup.nodes.Nodeto add before this element

Returns: this Element, for chaining

block?

boolean block? ( )
Java: isBlock() · also: is_block

Test 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)

child

Element child ( int index )
Java: child(int index)

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.

nametypedescription
indexintthe index number of the element to retrieve

Returns: the child element, if it exists, otherwise throws an IndexOutOfBoundsException

child_node_size

int child_node_size ( )
Java: childNodeSize()

children

Elements children ( )
Java: children()

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.

children_size

int children_size ( )
Java: 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

class_name

String class_name ( )
Java: 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.

class_names

Set<String> class_names ( )
Java: 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

class_names

Element class_names ( Set<String> class_names )
Java: classNames(Set<String> classNames)

Set the element's class attribute to the supplied class names.

nametypedescription
class_namesSet<String>set of classes

Returns: this element, for chaining

clear_attributes

Element clear_attributes ( )
Java: clearAttributes()

closest

org.jsoup.nodes.@Nullable Element closest ( String css_query )
Java: 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.

nametypedescription
css_queryStringa Selector CSS query

Returns: the closest ancestor element (possibly itself) that matches the provided evaluator. null if not found.

closest

org.jsoup.nodes.@Nullable Element closest ( Evaluator evaluator )
Java: 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.

nametypedescription
evaluatororg.jsoup.select.Evaluatora query evaluator

Returns: the closest ancestor element (possibly itself) that matches the provided evaluator. null if not found.

css_selector

String css_selector ( )
Java: 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

String data ( )
Java: 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

data_nodes

List<DataNode> data_nodes ( )
Java: dataNodes()

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

Map<String,String> dataset ( )
Java: 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.

element_is

boolean element_is ( String normal_name, String namespace )
Java: elementIs(String normalName, String namespace)

Test if this Element has the specified normalized name, and is in the specified namespace.

nametypedescription
normal_nameStringa normalized element name (e.g. div).
namespaceStringthe namespace

Returns: true if the element's normal name matches exactly, and is in the specified namespace

element_sibling_index

int element_sibling_index ( )
Java: 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

empty

Element empty ( )
Java: empty()

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

end_source_range

Range end_source_range ( )
Java: endSourceRange()

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.

expect_first

Element expect_first ( String css_query )
Java: expectFirst(String cssQuery)

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.

nametypedescription
css_queryStringa Selector CSS-like query

Returns: the first matching element

Throws

IllegalArgumentException if no match is found

expect_first_node

T expect_first_node ( String css_query, Class<T> type )
Java: 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.

nametypedescription
css_queryStringa Selector CSS-like query

Returns: the first matching node

Throws

IllegalArgumentException if no match is found

filter

Element filter ( NodeFilter node_filter )
Java: filter(org.jsoup.select.NodeFilter nodeFilter)

first_element_child

org.jsoup.nodes.@Nullable Element first_element_child ( )
Java: 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.

first_element_sibling

Element first_element_sibling ( )
Java: firstElementSibling()

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)

for_each

for_each ( Consumer<? super Element> action )
Java: 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.

nametypedescription
actionjava.util.function.Consumer<? super org.jsoup.nodes.Element>the function to perform on the element

for_each_node

Element for_each_node ( Consumer<? super Node> action )
Java: forEachNode(java.util.function.Consumer<? super org.jsoup.nodes.Node> action)

get_element_by_id

org.jsoup.nodes.@Nullable Element get_element_by_id ( String id )
Java: 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)

nametypedescription
idStringThe ID to search for.

Returns: The first matching element by ID, starting with this element, or null if none found.

get_elements_by_attribute

Elements get_elements_by_attribute ( String key )
Java: getElementsByAttribute(String key)

Find elements that have a named attribute set. Case-insensitive.

nametypedescription
keyStringname of the attribute, e.g. href

Returns: elements that have this attribute, empty if none

get_elements_by_attribute_starting

Elements get_elements_by_attribute_starting ( String key_prefix )
Java: getElementsByAttributeStarting(String keyPrefix)

Find elements that have an attribute name starting with the supplied prefix. Use data- to find elements that have HTML5 datasets.

nametypedescription
key_prefixStringname prefix of the attribute e.g. data-

Returns: elements that have attribute names that start with the prefix, empty if none.

get_elements_by_attribute_value

Elements get_elements_by_attribute_value ( String key, String value )
Java: getElementsByAttributeValue(String key, String value)

Find elements that have an attribute with the specific value. Case-insensitive.

nametypedescription
keyStringname of the attribute
valueStringvalue of the attribute

Returns: elements that have this attribute with this value, empty if none

get_elements_by_attribute_value_containing

Elements get_elements_by_attribute_value_containing ( String key, String match )
Java: getElementsByAttributeValueContaining(String key, String match)

Find elements that have attributes whose value contains the match string. Case-insensitive.

nametypedescription
keyStringname of the attribute
matchStringsubstring of value to search for

Returns: elements that have attributes containing this text

get_elements_by_attribute_value_ending

Elements get_elements_by_attribute_value_ending ( String key, String value_suffix )
Java: getElementsByAttributeValueEnding(String key, String valueSuffix)

Find elements that have attributes that end with the value suffix. Case-insensitive.

nametypedescription
keyStringname of the attribute
value_suffixStringend of the attribute value

Returns: elements that have attributes that end with the value suffix

get_elements_by_attribute_value_matching

Elements get_elements_by_attribute_value_matching ( String key, Pattern pattern )
Java: getElementsByAttributeValueMatching(String key, java.util.regex.Pattern pattern)

Find elements that have an attribute whose value matches the supplied regular expression.

nametypedescription
keyStringname of the attribute
patternjava.util.regex.Patterncompiled regular expression to match against attribute values

Returns: elements that have attributes matching this regular expression

get_elements_by_attribute_value_matching

Elements get_elements_by_attribute_value_matching ( String key, String regex )
Java: getElementsByAttributeValueMatching(String key, String regex)

Find elements that have attributes whose values match the supplied regular expression.

nametypedescription
keyStringname of the attribute
regexStringregular 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

get_elements_by_attribute_value_not

Elements get_elements_by_attribute_value_not ( String key, String value )
Java: getElementsByAttributeValueNot(String key, String value)

Find elements that either do not have this attribute, or have it with a different value. Case-insensitive.

nametypedescription
keyStringname of the attribute
valueStringvalue of the attribute

Returns: elements that do not have a matching attribute

get_elements_by_attribute_value_starting

Elements get_elements_by_attribute_value_starting ( String key, String value_prefix )
Java: getElementsByAttributeValueStarting(String key, String valuePrefix)

Find elements that have attributes that start with the value prefix. Case-insensitive.

nametypedescription
keyStringname of the attribute
value_prefixStringstart of attribute value

Returns: elements that have attributes that start with the value prefix

get_elements_by_class

Elements get_elements_by_class ( String class_name )
Java: getElementsByClass(String className)

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");.

nametypedescription
class_nameStringthe name of the class to search for.

Returns: elements with the supplied class name, empty if none

get_elements_by_index_equals

Elements get_elements_by_index_equals ( int index )
Java: getElementsByIndexEquals(int index)

Find elements whose sibling index is equal to the supplied index.

nametypedescription
indexint0-based index

Returns: elements equal to index

get_elements_by_index_greater_than

Elements get_elements_by_index_greater_than ( int index )
Java: getElementsByIndexGreaterThan(int index)

Find elements whose sibling index is greater than the supplied index.

nametypedescription
indexint0-based index

Returns: elements greater than index

get_elements_by_index_less_than

Elements get_elements_by_index_less_than ( int index )
Java: getElementsByIndexLessThan(int index)

Find elements whose sibling index is less than the supplied index.

nametypedescription
indexint0-based index

Returns: elements less than index

get_elements_by_tag

Elements get_elements_by_tag ( String tag_name )
Java: getElementsByTag(String tagName)

Finds elements, including and recursively under this element, with the specified tag name.

nametypedescription
tag_nameStringThe 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.

get_elements_containing_own_text

Elements get_elements_containing_own_text ( String search_text )
Java: 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.

nametypedescription
search_textStringto look for in the element's own text

Returns: elements that contain the string, case-insensitive.

get_elements_containing_text

Elements get_elements_containing_text ( String search_text )
Java: 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.

nametypedescription
search_textStringto look for in the element's text

Returns: elements that contain the string, case-insensitive.

get_elements_matching_own_text

Elements get_elements_matching_own_text ( Pattern pattern )
Java: getElementsMatchingOwnText(java.util.regex.Pattern pattern)

Find elements whose own text matches the supplied regular expression.

nametypedescription
patternjava.util.regex.Patternregular expression to match text against

Returns: elements matching the supplied regular expression.

get_elements_matching_own_text

Elements get_elements_matching_own_text ( String regex )
Java: getElementsMatchingOwnText(String regex)

Find elements whose own text matches the supplied regular expression.

nametypedescription
regexStringregular 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.

get_elements_matching_text

Elements get_elements_matching_text ( Pattern pattern )
Java: getElementsMatchingText(java.util.regex.Pattern pattern)

Find elements whose text matches the supplied regular expression.

nametypedescription
patternjava.util.regex.Patternregular expression to match text against

Returns: elements matching the supplied regular expression.

get_elements_matching_text

Elements get_elements_matching_text ( String regex )
Java: getElementsMatchingText(String regex)

Find elements whose text matches the supplied regular expression.

nametypedescription
regexStringregular 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.

has_class

boolean has_class ( String class_name )
Java: hasClass(String className)

Tests if this element has a class. Case-insensitive.

nametypedescription
class_nameStringname of class to check for

Returns: true if it does, false if not

has_text?

boolean has_text? ( )
Java: hasText() · also: has_text

Checks 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

String html ( )
Java: 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 html ( T accum )
Java: html(T accum)

html

Element html ( String html )
Java: html(String html)

Set this element's inner HTML. Clears the existing HTML first.

nametypedescription
htmlStringHTML to parse and set into this element

Returns: this element

id

String id ( )
Java: id()

Get the id attribute of this element.

Returns: The id attribute, if present, or an empty string if not.

id

Element id ( String id )
Java: id(String id)

Set the id attribute of this element.

nametypedescription
idStringthe ID value to use

Returns: this Element, for chaining

insert_children

Element insert_children ( int index, Collection<? extends Node> children )
Java: 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.

nametypedescription
indexint0-based index to insert children at. Specify 0 to insert at the start, -1 at the end
childrenCollection<? extends org.jsoup.nodes.Node>child nodes to insert

Returns: this element, for chaining.

insert_children

Element insert_children ( int index, Node[] children )
Java: 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.

nametypedescription
indexint0-based index to insert children at. Specify 0 to insert at the start, -1 at the end
childrenorg.jsoup.nodes.Node[]child nodes to insert

Returns: this element, for chaining.

is

boolean is ( String css_query )
Java: is(String cssQuery)

Checks if this element matches the given Selector CSS query. Also knows as matches() in the Web DOM.

nametypedescription
css_queryStringa Selector CSS query

Returns: if this element matches the query

is

boolean is ( Evaluator evaluator )
Java: is(org.jsoup.select.Evaluator evaluator)

Check if this element matches the given evaluator.

nametypedescription
evaluatororg.jsoup.select.Evaluatoran element evaluator

Returns: if this element matches

iterator

Iterator<Element> iterator ( )
Java: iterator()

Returns an Iterator that iterates this Element and each of its descendant Elements, in document order.

Returns: an Iterator

last_element_child

org.jsoup.nodes.@Nullable Element last_element_child ( )
Java: 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.

last_element_sibling

Element last_element_sibling ( )
Java: lastElementSibling()

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)

next_element_siblings

Elements next_element_siblings ( )
Java: nextElementSiblings()

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

node_name

String node_name ( )
Java: nodeName()

node_value

String node_value ( )
Java: nodeValue()

An Element's nodeValue is its whole own text.

normal_name

String normal_name ( )
Java: 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

own_text

String own_text ( )
Java: 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

org.jsoup.nodes.@Nullable Element parent ( )
Java: parent()

parents

Elements parents ( )
Java: parents()

Get this element's parent and ancestors, up to the document root.

Returns: this element's stack of parents, starting with the closest first.

prepend

Element prepend ( String html )
Java: prepend(String html)

Add inner HTML into this element. The supplied HTML will be parsed, and each node prepended to the start of the element's children.

nametypedescription
htmlStringHTML to add inside this element, before the existing HTML

Returns: this element

prepend_child

Element prepend_child ( Node child )
Java: prependChild(org.jsoup.nodes.Node child)

Add a node to the start of this element's children.

nametypedescription
childorg.jsoup.nodes.Nodenode to add.

Returns: this element, so that you can add more child nodes or elements.

prepend_children

Element prepend_children ( Collection<? extends Node> children )
Java: prependChildren(Collection<? extends org.jsoup.nodes.Node> children)

Insert the given nodes to the start of this Element's children.

nametypedescription
childrenCollection<? extends org.jsoup.nodes.Node>nodes to add

Returns: this Element, for chaining

prepend_element

Element prepend_element ( String tag_name )
Java: prependElement(String tagName)

Create a new element by tag name, and add it as this Element's first child.

nametypedescription
tag_nameStringthe 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");

prepend_element

Element prepend_element ( String tag_name, String namespace )
Java: prependElement(String tagName, String namespace)

Create a new element by tag name and namespace, and add it as this Element's first child.

nametypedescription
tag_nameStringthe name of the tag (e.g. div).
namespaceStringthe namespace of the tag (e.g. Parser#NamespaceHtml)

Returns: the new element, in the specified namespace

prepend_text

Element prepend_text ( String text )
Java: prependText(String text)

Create and prepend a new TextNode to this element.

nametypedescription
textStringthe decoded text to add

Returns: this element

previous_element_siblings

Elements previous_element_siblings ( )
Java: previousElementSiblings()

Get each of the element siblings before this element.

Returns: the previous element siblings, or an empty list if there are none.

remove_attr

Element remove_attr ( String attribute_key )
Java: removeAttr(String attributeKey)

remove_class

Element remove_class ( String class_name )
Java: removeClass(String className)

Remove a class name from this element's class attribute.

nametypedescription
class_nameStringclass name to remove

Returns: this element

root

Element root ( )
Java: root()

select

Elements select ( String css_query )
Java: select(String cssQuery)

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.

nametypedescription
css_queryStringa Selector CSS-like query

Returns: an Elements list containing elements that match the query (empty if none match)

Throws

Selector.SelectorParseException (unchecked) on an invalid CSS query.

select

Elements select ( Evaluator evaluator )
Java: select(org.jsoup.select.Evaluator evaluator)

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.

nametypedescription
evaluatororg.jsoup.select.Evaluatoran element evaluator

Returns: an Elements list containing elements that match the query (empty if none match)

select_first

org.jsoup.nodes.@Nullable Element select_first ( String css_query )
Java: 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.

nametypedescription
css_queryStringcssQuery a Selector CSS-like query

Returns: the first matching element, or null if there is no match.

select_first

org.jsoup.nodes.@Nullable Element select_first ( Evaluator evaluator )
Java: 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.

nametypedescription
evaluatororg.jsoup.select.Evaluatoran element evaluator

Returns: the first matching element (walking down the tree, starting from this element), or null if none match.

select_first_node

@Nullable T select_first_node ( String css_query, Class<T> type )
Java: 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.

nametypedescription
css_queryStringcssQuery a Selector CSS-like query

Returns: the first matching node, or null if there is no match.

select_first_node

@Nullable T select_first_node ( Evaluator evaluator, Class<T> type )
Java: 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.

nametypedescription
evaluatororg.jsoup.select.Evaluatoran element evaluator

Returns: the first matching node (walking down the tree, starting from this element), or null if none match.

select_nodes

Nodes<Node> select_nodes ( Evaluator evaluator )
Java: 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.

nametypedescription
evaluatororg.jsoup.select.Evaluatoran evaluator

Returns: a list of nodes that match the query (empty if none match)

select_nodes

Nodes<Node> select_nodes ( String css_query )
Java: selectNodes(String cssQuery)

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.

nametypedescription
css_queryStringa Selector CSS query

Returns: a list of nodes that match the query (empty if none match)

select_nodes

Nodes<T> select_nodes ( Evaluator evaluator, Class<T> type )
Java: 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.

nametypedescription
evaluatororg.jsoup.select.Evaluatoran evaluator
typeClass<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)

select_nodes

Nodes<T> select_nodes ( String css_query, Class<T> type )
Java: 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);

nametypedescription
css_queryStringa Selector CSS query
typeClass<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)

select_stream

Stream<Element> select_stream ( String css_query )
Java: selectStream(String cssQuery)

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.

nametypedescription
css_queryStringa Selector CSS-like query

Returns: a Stream containing elements that match the query (empty if none match)

Throws

Selector.SelectorParseException (unchecked) on an invalid CSS query.

select_stream

Stream<Element> select_stream ( Evaluator evaluator )
Java: selectStream(org.jsoup.select.Evaluator evaluator)

Find a Stream of elements that match the supplied Evaluator.

nametypedescription
evaluatororg.jsoup.select.Evaluatoran element Evaluator

Returns: a Stream containing elements that match the query (empty if none match)

select_xpath

Elements select_xpath ( String xpath )
Java: selectXpath(String xpath)

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:

  1. Add the implementation to your classpath. E.g. to use Saxon-HE, add net.sf.saxon:Saxon-HE to your build.
  2. Set the system property javax.xml.xpath.XPathFactory:jsoup to the implementing classname. E.g.:
    System.setProperty(W3CDom.XPathFactoryProperty, "net.sf.saxon.xpath.XPathFactoryImpl");

nametypedescription
xpathStringXPath expression

Returns: matching elements, or an empty list if none match.

select_xpath

List<T> select_xpath ( String xpath, Class<T> node_type )
Java: 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");

nametypedescription
xpathStringXPath expression
node_typeClass<T>the jsoup node type to return

Returns: a list of matching nodes

shallow_clone

Element shallow_clone ( )
Java: shallowClone()

sibling_elements

Elements sibling_elements ( )
Java: siblingElements()

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

stream

Stream<Element> stream ( )
Java: stream()

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.

tag

Tag tag ( )
Java: tag()

Get the Tag for this element.

Returns: the tag object

tag

Element tag ( Tag tag )
Java: tag(org.jsoup.parser.Tag tag)

Change the Tag of this element.

nametypedescription
tagorg.jsoup.parser.Tagthe new tag

Returns: this element, for chaining

tag_name

String tag_name ( )
Java: 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

tag_name

Element tag_name ( String tag_name )
Java: tagName(String tagName)

Change (rename) the tag of this element. For example, convert a <span> to a <div> with el.tagName("div");.

nametypedescription
tag_nameStringnew tag name for this element

Returns: this element, for chaining

tag_name

Element tag_name ( String tag_name, String namespace )
Java: tagName(String tagName, String namespace)

Change (rename) the tag of this element. For example, convert a <span> to a <div> with el.tagName("div");.

nametypedescription
tag_nameStringnew tag name for this element
namespaceStringthe new namespace for this element

Returns: this element, for chaining

text

String text ( )
Java: 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.

text

Element text ( String text )
Java: text(String text)

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.

nametypedescription
textStringdecoded text

Returns: this element

text_nodes

List<TextNode> text_nodes ( )
Java: textNodes()

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_class

Element toggle_class ( String class_name )
Java: toggleClass(String className)

Toggle a class name on this element's class attribute: if present, remove it; otherwise add it.

nametypedescription
class_nameStringclass name to toggle

Returns: this element

traverse

Element traverse ( NodeVisitor node_visitor )
Java: traverse(org.jsoup.select.NodeVisitor nodeVisitor)

val

String val ( )
Java: val()

Get the value of a form element (input, textarea, etc).

Returns: the value of the form element, or empty string if not set.

val

Element val ( String value )
Java: val(String value)

Set the value of a form element (input, textarea, etc).

nametypedescription
valueStringvalue to set

Returns: this element (for chaining)

whole_own_text

String whole_own_text ( )
Java: 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

whole_text

String whole_text ( )
Java: 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

Element wrap ( String html )
Java: wrap(String html)

Wrap the supplied HTML around this element.

nametypedescription
htmlStringHTML to wrap around this element, e.g. <div class="head"></div>. Can be arbitrarily deep.

Returns: this element, for chaining.