|  | Home · All Classes · Modules

# QWebElement Class Reference
[QtWebKit module]

The QWebElement class provides convenient access to DOM elements in a QWebFrame. More...

### Types

- enum StyleResolveStrategy { InlineStyle, CascadedStyle, ComputedStyle }

### Methods

- __init__ (self)
- __init__ (self, QWebElement)
- addClass (self, QString)
- appendInside (self, QString)
- appendInside (self, QWebElement)
- appendOutside (self, QString)
- appendOutside (self, QWebElement)
- QString attribute (self, QString, QString defaultValue = QString())
- QStringList attributeNames (self, QString namespaceUri = QString())
- QString attributeNS (self, QString, QString, QString defaultValue = QString())
- QStringList classes (self)
- QWebElement clone (self)
- QWebElement document (self)
- encloseContentsWith (self, QWebElement)
- encloseContentsWith (self, QString)
- encloseWith (self, QString)
- encloseWith (self, QWebElement)
- QVariant evaluateJavaScript (self, QString)
- QWebElementCollection findAll (self, QString)
- QWebElement findFirst (self, QString)
- QWebElement firstChild (self)
- QRect geometry (self)
- bool hasAttribute (self, QString)
- bool hasAttributeNS (self, QString, QString)
- bool hasAttributes (self)
- bool hasClass (self, QString)
- bool hasFocus (self)
- bool isNull (self)
- QWebElement lastChild (self)
- QString localName (self)
- QString namespaceUri (self)
- QWebElement nextSibling (self)
- QWebElement parent (self)
- QString prefix (self)
- prependInside (self, QString)
- prependInside (self, QWebElement)
- prependOutside (self, QString)
- prependOutside (self, QWebElement)
- QWebElement previousSibling (self)
- removeAllChildren (self)
- removeAttribute (self, QString)
- removeAttributeNS (self, QString, QString)
- removeClass (self, QString)
- removeFromDocument (self)
- render (self, QPainter)
- replace (self, QString)
- replace (self, QWebElement)
- setAttribute (self, QString, QString)
- setAttributeNS (self, QString, QString, QString)
- setFocus (self)
- setInnerXml (self, QString)
- setOuterXml (self, QString)
- setPlainText (self, QString)
- setStyleProperty (self, QString, QString)
- QString styleProperty (self, QString, StyleResolveStrategy)
- QString tagName (self)
- QWebElement takeFromDocument (self)
- toggleClass (self, QString)
- QString toInnerXml (self)
- QString toOuterXml (self)
- QString toPlainText (self)
- QWebFrame webFrame (self)

### Special Methods

- bool __eq__ (self, QWebElement)
- bool __ne__ (self, QWebElement)

## Detailed Description

The QWebElement class provides convenient access to DOM elements in a QWebFrame.

A QWebElement object allows easy access to the document model, represented by a tree-like structure of DOM elements. The root of the tree is called the document element and can be accessed using QWebFrame.documentElement().

Specific elements can be accessed using findAll() and findFirst(). These elements are identified using CSS selectors. The code snippet below demonstrates the use of findAll().

         QWebElement document = frame->documentElement();
         /* Assume the document has the following structure:

            <p class=intro>
              <span>Intro</span>
              <span>Snippets</span>
            </p>
            <p>
              <span>Content</span>
              <span>Here</span>
            </p>
         */

         QList<QWebElement> allSpans = document.findAll("span");
         QList<QWebElement> introSpans = document.findAll("p.intro span");

The first list contains all `span` elements in the document. The second list contains `span` elements that are children of `p`, classified with `intro`.

Using findFirst() is more efficient than calling findAll(), and extracting the first element only in the list returned.

Alternatively you can traverse the document manually using firstChild() and nextSibling():

         frame->setHtml("<html><body><p>First Paragraph</p><p>Second Paragraph</p></body></html>");
         QWebElement doc = frame->documentElement();
         QWebElement body = doc.firstChild();
         QWebElement firstParagraph = body.firstChild();
         QWebElement secondParagraph = firstParagraph.nextSibling();

The underlying content of QWebElement is explicitly shared. Creating a copy of a QWebElement does not create a copy of the content. Instead, both instances point to the same element.

The element's attributes can be read using attribute() and modified with setAttribute().

The contents of child elements can be converted to plain text with toPlainText(); to XHTML using toInnerXml(). To include the element's tag in the output, use toOuterXml().

It is possible to replace the contents of child elements using setPlainText() and setInnerXml(). To replace the element itself and its contents, use setOuterXml().

### Examples

The DOM Traversal Example shows one way to traverse documents in a running example.

The Simple Selector Example can be used to experiment with the searching features of this class and provides sample code you can start working with.

## Type Documentation

### QWebElement.StyleResolveStrategy

This enum describes how QWebElement's styleProperty resolves the given property name.

Constant | Value | Description
--- | --- | ---
`QWebElement.InlineStyle` | `0` | Return the property value as it is defined in the element, without respecting style inheritance and other CSS rules.
`QWebElement.CascadedStyle` | `1` | The property's value is determined using the inheritance and importance rules defined in the document's stylesheet.
`QWebElement.ComputedStyle` | `2` | The property's value is the absolute value of the style property resolved from the environment.

## Method Documentation

### QWebElement.__init__ (self)

Constructs a null web element.

### QWebElement.__init__ (self, QWebElement)

Constructs a copy of other.

### QWebElement.addClass (self, QString)

Adds the specified class with the given name to the element.

### QWebElement.appendInside (self, QString)

Appends the result of parsing markup as the element's last child.

Calling this function on a null element does nothing.

See also prependInside(), prependOutside(), and appendOutside().

### QWebElement.appendInside (self, QWebElement)

Appends the given element as the element's last child.

If element is the child of another element, it is re-parented to this element. If element is a child of this element, then its position in the list of children is changed.

Calling this function on a null element does nothing.

See also prependInside(), prependOutside(), and appendOutside().

### QWebElement.appendOutside (self, QString)

Inserts the result of parsing markup after this element.

Calling this function on a null element does nothing.

See also appendInside(), prependInside(), and prependOutside().

### QWebElement.appendOutside (self, QWebElement)

Inserts the given element after this element.

If element is the child of another element, it is re-parented to the parent of this element.

Calling this function on a null element does nothing.

See also appendInside(), prependInside(), and prependOutside().

### QString QWebElement.attribute (self, QString, QString defaultValue = QString())

Returns the attribute with the given name. If the attribute does not exist, defaultValue is returned.

See also setAttribute(), setAttributeNS(), and attributeNS().

### QStringList QWebElement.attributeNames (self, QString namespaceUri = QString())

Return the list of attributes for the namespace given as namespaceUri.

See also attribute() and setAttribute().

### QString QWebElement.attributeNS (self, QString, QString, QString defaultValue = QString())

Returns the attribute with the given name in namespaceUri. If the attribute does not exist, defaultValue is returned.

See also setAttributeNS(), setAttribute(), and attribute().

### QStringList QWebElement.classes (self)

Returns the list of classes of this element.

### QWebElement QWebElement.clone (self)

Returns a clone of this element.

The clone may be inserted at any point in the document.

See also appendInside(), prependInside(), prependOutside(), and appendOutside().

### QWebElement QWebElement.document (self)

Returns the document which this element belongs to.

### QWebElement.encloseContentsWith (self, QWebElement)

Encloses the contents of this element with element. This element becomes the child of the deepest descendant within element.

### illustration

See also encloseWith().

### QWebElement.encloseContentsWith (self, QString)

Encloses the contents of this element with the result of parsing markup. This element becomes the child of the deepest descendant within markup.

See also encloseWith().

### QWebElement.encloseWith (self, QString)

Encloses this element with the result of parsing markup. This element becomes the child of the deepest descendant within markup.

See also replace().

### QWebElement.encloseWith (self, QWebElement)

Encloses this element with element. This element becomes the child of the deepest descendant within element.

See also replace().

### QVariant QWebElement.evaluateJavaScript (self, QString)

Executes scriptSource with this element as `this` object.

### QWebElementCollection QWebElement.findAll (self, QString)

Returns a new list of child elements matching the given CSS selector selectorQuery. If there are no matching elements, an empty list is returned.

Standard CSS2 selector syntax is used for the query.

Note: This search is performed recursively.

See also findFirst().

### QWebElement QWebElement.findFirst (self, QString)

Returns the first child element that matches the given CSS selector selectorQuery.

Standard CSS2 selector syntax is used for the query.

Note: This search is performed recursively.

See also findAll().

### QWebElement QWebElement.firstChild (self)

Returns the element's first child.

See also lastChild(), previousSibling(), and nextSibling().

### QRect QWebElement.geometry (self)

Returns the geometry of this element, relative to its containing frame.

See also tagName().

### bool QWebElement.hasAttribute (self, QString)

Returns true if this element has an attribute with the given name; otherwise returns false.

See also attribute() and setAttribute().

### bool QWebElement.hasAttributeNS (self, QString, QString)

Returns true if this element has an attribute with the given name, in namespaceUri; otherwise returns false.

See also attributeNS() and setAttributeNS().

### bool QWebElement.hasAttributes (self)

Returns true if the element has any attributes defined; otherwise returns false;

See also attribute() and setAttribute().

### bool QWebElement.hasClass (self, QString)

Returns true if this element has a class with the given name; otherwise returns false.

### bool QWebElement.hasFocus (self)

Returns true if the element has keyboard input focus; otherwise, returns false

See also setFocus().

### bool QWebElement.isNull (self)

Returns true if the element is a null element; otherwise returns false.

### QWebElement QWebElement.lastChild (self)

Returns the element's last child.

See also firstChild(), previousSibling(), and nextSibling().

### QString QWebElement.localName (self)

Returns the local name of the element. If the element does not use namespaces, an empty string is returned.

### QString QWebElement.namespaceUri (self)

### QWebElement QWebElement.nextSibling (self)

Returns the element's next sibling.

See also firstChild(), previousSibling(), and lastChild().

### QWebElement QWebElement.parent (self)

Returns the parent element of this elemen. If this element is the root document element, a null element is returned.

### QString QWebElement.prefix (self)

Returns the namespace prefix of the element. If the element has no namespace prefix, empty string is returned.

### QWebElement.prependInside (self, QString)

Prepends the result of parsing markup as the element's first child.

Calling this function on a null element does nothing.

See also appendInside(), prependOutside(), and appendOutside().

### QWebElement.prependInside (self, QWebElement)

Prepends element as the element's first child.

If element is the child of another element, it is re-parented to this element. If element is a child of this element, then its position in the list of children is changed.

Calling this function on a null element does nothing.

See also appendInside(), prependOutside(), and appendOutside().

### QWebElement.prependOutside (self, QString)

Inserts the result of parsing markup before this element.

Calling this function on a null element does nothing.

See also appendInside(), prependInside(), and appendOutside().

### QWebElement.prependOutside (self, QWebElement)

Inserts the given element before this element.

If element is the child of another element, it is re-parented to the parent of this element.

Calling this function on a null element does nothing.

See also appendInside(), prependInside(), and appendOutside().

### QWebElement QWebElement.previousSibling (self)

Returns the element's previous sibling.

See also firstChild(), nextSibling(), and lastChild().

### QWebElement.removeAllChildren (self)

Removes all children from this element.

See also removeFromDocument() and takeFromDocument().

### QWebElement.removeAttribute (self, QString)

Removes the attribute with the given name from this element.

See also attribute(), setAttribute(), and hasAttribute().

### QWebElement.removeAttributeNS (self, QString, QString)

Removes the attribute with the given name, in namespaceUri, from this element.

See also attributeNS(), setAttributeNS(), and hasAttributeNS().

### QWebElement.removeClass (self, QString)

Removes the specified class with the given name from the element.

### QWebElement.removeFromDocument (self)

Removes this element from the document and makes it a null element.

See also removeAllChildren() and takeFromDocument().

### QWebElement.render (self, QPainter)

Render the element into painter .

### QWebElement.replace (self, QString)

Replaces this element with the result of parsing markup.

This method will not replace the <html>, <head> or <body> elements.

See also encloseWith().

### QWebElement.replace (self, QWebElement)

Replaces this element with element.

This method will not replace the <html>, <head> or <body> elements.

See also encloseWith().

### QWebElement.setAttribute (self, QString, QString)

Adds an attribute with the given name and value. If an attribute with the same name exists, its value is replaced by value.

See also attribute(), attributeNS(), and setAttributeNS().

### QWebElement.setAttributeNS (self, QString, QString, QString)

Adds an attribute with the given name in namespaceUri with value. If an attribute with the same name exists, its value is replaced by value.

See also attributeNS(), attribute(), and setAttribute().

### QWebElement.setFocus (self)

Gives keyboard input focus to this element

See also hasFocus().

### QWebElement.setInnerXml (self, QString)

Replaces the contents of this element with markup. The string may contain HTML or XML tags, which is parsed and formatted before insertion into the document.

Note: This is currently implemented for (X)HTML elements only.

See also toInnerXml(), toOuterXml(), and setOuterXml().

### QWebElement.setOuterXml (self, QString)

Replaces the contents of this element as well as its own tag with markup. The string may contain HTML or XML tags, which is parsed and formatted before insertion into the document.

Note: This is currently only implemented for (X)HTML elements.

See also toOuterXml(), toInnerXml(), and setInnerXml().

### QWebElement.setPlainText (self, QString)

Replaces the existing content of this element with text.

This is equivalent to setting the HTML innerText property.

See also toPlainText().

### QWebElement.setStyleProperty (self, QString, QString)

Sets the value of the inline style with the given name to value.

Setting a value, does not necessarily mean that it will become the applied value, due to the fact that the style property's value might have been set earlier with a higher priority in external or embedded style declarations.

In order to ensure that the value will be applied, you may have to append "!important" to the value.

See also styleProperty().

### QString QWebElement.styleProperty (self, QString, StyleResolveStrategy)

Returns the value of the style with the given name using the specified strategy. If a style with name does not exist, an empty string is returned.

In CSS, the cascading part depends on which CSS rule has priority and is thus applied. Generally, the last defined rule has priority. Thus, an inline style rule has priority over an embedded block style rule, which in return has priority over an external style rule.

If the "!important" declaration is set on one of those, the declaration receives highest priority, unless other declarations also use the "!important" declaration. Then, the last "!important" declaration takes predecence.

See also setStyleProperty().

### QString QWebElement.tagName (self)

Returns the tag name of this element.

See also geometry().

### QWebElement QWebElement.takeFromDocument (self)

Removes this element from the document and returns a reference to it.

The element is still valid after removal, and can be inserted into other parts of the document.

See also removeAllChildren() and removeFromDocument().

### QWebElement.toggleClass (self, QString)

Adds the specified class with the given name if it is not present. If the class is already present, it will be removed.

### QString QWebElement.toInnerXml (self)

Returns the XML content between the element's start and end tags.

Note: This is currently implemented for (X)HTML elements only.

See also setInnerXml(), setOuterXml(), and toOuterXml().

### QString QWebElement.toOuterXml (self)

Returns this element converted to XML, including the start and the end tags as well as its attributes.

Note: This is currently implemented for (X)HTML elements only.

See also setOuterXml(), setInnerXml(), and toInnerXml().

### QString QWebElement.toPlainText (self)

Returns the text between the start and the end tag of this element.

This is equivalent to reading the HTML innerText property.

See also setPlainText().

### QWebFrame QWebElement.webFrame (self)

Returns the web frame which this element is a part of. If the element is a null element, null is returned.

### bool QWebElement.__eq__ (self, QWebElement)

### bool QWebElement.__ne__ (self, QWebElement)