QWebElement Class Reference

[ QtWebKit module]

The QWebElement class provides convenient access to DOM elements 在 QWebFrame . 更多...

类型

方法

Special Methods


详细描述

The QWebElement class provides convenient access to DOM elements 在 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 () 和 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>
     */
     QWebElementCollection allSpans = document.findAll("span");
     QWebElementCollection 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 .

使用 findFirst () 是 more efficient than calling findAll (), and extracting the first element only in the list returned.

Alternatively you can traverse the document manually using firstChild () 和 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();
			

Individual elements can be inspected or changed using methods such as attribute () 或 setAttribute (). For examle, to capture the user's input in a text field for later use (auto-completion), a browser could do something like this:

     QWebElement firstTextInput = document.findFirst("input[type=text]");
     QString storedText = firstTextInput.attribute("value");
			

When the same page is later revisited, the browser can fill in the text field automatically by modifying the value attribute of the input element:

     QWebElement firstTextInput = document.findFirst("input[type=text]");
     textInput.setAttribute("value", storedText);
			

Another use case is to emulate a click event on an element. The following code snippet demonstrates how to call the JavaScript DOM method click() of a submit button:

     QWebElement document = frame->documentElement();
     /* Assume that the document has the following structure:
         <form name="myform" action="submit_form.asp" method="get">
             <input type="text" name="myfield">
             <input type="submit" value="Submit">
         </form>
      */
     QWebElement button = document.findFirst("input[type=submit]");
     button.evaluateJavaScript("this.click()");
			

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 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 () 和 setInnerXml (). To replace the element itself and its contents, use setOuterXml ().

范例

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

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


类型文档编制

QWebElement.StyleResolveStrategy

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

常量 描述
QWebElement.InlineStyle 0 Return the property value as it is defined in the element, without respecting style inheritance and other CSS 规则。
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.

方法文档编制

QWebElement.__init__ ( self )

Constructs a null web element.

QWebElement.__init__ ( self , QWebElement )

构造副本为 other .

QWebElement.addClass ( self , QString  name )

Adds the specified class with the given name 到 元素。

QWebElement.appendInside ( self , QString  markup )

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

Calling this function on a null element does nothing.

另请参阅 prependInside (), prependOutside (),和 appendOutside ().

QWebElement.appendInside ( self , QWebElement   element )

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

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.

另请参阅 prependInside (), prependOutside (),和 appendOutside ().

QWebElement.appendOutside ( self , QString  markup )

Inserts the result of parsing markup after this 元素。

Calling this function on a null element does nothing.

另请参阅 appendInside (), prependInside (),和 prependOutside ().

QWebElement.appendOutside ( self , QWebElement   element )

Inserts the given element after this element.

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.

另请参阅 appendInside (), prependInside (),和 prependOutside ().

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

Returns the attribute with the given name 。若 attribute does not exist, defaultValue 被返回。

另请参阅 setAttribute (), setAttributeNS (),和 attributeNS ().

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

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

另请参阅 attribute () 和 setAttribute ().

QString QWebElement.attributeNS ( self , QString  namespaceUri , QString  name , QString  defaultValue  = QString())

Returns the attribute with the given name in namespaceUri . If the attribute does not exist, defaultValue 被返回。

另请参阅 setAttributeNS (), setAttribute (),和 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.

另请参阅 appendInside (), prependInside (), prependOutside (),和 appendOutside ().

QWebElement QWebElement.document ( self )

Returns the document which this element belongs to.

QWebElement.encloseContentsWith ( self , QWebElement   element )

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

### illustration

另请参阅 encloseWith ().

QWebElement.encloseContentsWith ( self , QString  markup )

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

另请参阅 encloseWith ().

QWebElement.encloseWith ( self , QString  markup )

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

另请参阅 replace ().

QWebElement.encloseWith ( self , QWebElement   element )

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

另请参阅 replace ().

QVariant QWebElement.evaluateJavaScript ( self , QString  scriptSource )

Executes scriptSource with this element as this 对象。

QWebElementCollection QWebElement.findAll ( self , QString  selectorQuery )

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.

注意: This search is performed recursively.

另请参阅 findFirst ().

QWebElement QWebElement.findFirst ( self , QString  selectorQuery )

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

Standard CSS2 selector syntax is used for the query.

注意: This search is performed recursively.

另请参阅 findAll ().

QWebElement QWebElement.firstChild ( self )

Returns the element's first child.

另请参阅 lastChild (), previousSibling (),和 nextSibling ().

QRect QWebElement.geometry ( self )

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

另请参阅 tagName ().

bool QWebElement.hasAttribute ( self , QString  name )

Returns true if this element has an attribute with the given name ;否则返回 false。

另请参阅 attribute () 和 setAttribute ().

bool QWebElement.hasAttributeNS ( self , QString  namespaceUri , QString  name )

Returns true if this element has an attribute with the given name , in namespaceUri ;否则返回 false。

另请参阅 attributeNS () 和 setAttributeNS ().

bool QWebElement.hasAttributes ( self )

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

另请参阅 attribute () 和 setAttribute ().

bool QWebElement.hasClass ( self , QString  name )

Returns true if this element has a class with the given name ;否则返回 false。

bool QWebElement.hasFocus ( self )

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

另请参阅 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.

另请参阅 firstChild (), previousSibling (),和 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 )

Returns the namespace URI of this element. If the element has no namespace URI, an empty string is returned.

QWebElement QWebElement.nextSibling ( self )

Returns the element's next sibling.

另请参阅 firstChild (), previousSibling (),和 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  markup )

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

Calling this function on a null element does nothing.

另请参阅 appendInside (), prependOutside (),和 appendOutside ().

QWebElement.prependInside ( self , QWebElement   element )

前置 element as the element's first child.

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.

另请参阅 appendInside (), prependOutside (),和 appendOutside ().

QWebElement.prependOutside ( self , QString  markup )

Inserts the result of parsing markup before this 元素。

Calling this function on a null element does nothing.

另请参阅 appendInside (), prependInside (),和 appendOutside ().

QWebElement.prependOutside ( self , QWebElement   element )

Inserts the given element before this element.

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.

另请参阅 appendInside (), prependInside (),和 appendOutside ().

QWebElement QWebElement.previousSibling ( self )

Returns the element's previous sibling.

另请参阅 firstChild (), nextSibling (),和 lastChild ().

QWebElement.removeAllChildren ( self )

Removes all children from this element.

另请参阅 removeFromDocument () 和 takeFromDocument ().

QWebElement.removeAttribute ( self , QString  name )

Removes the attribute with the given name 从此 元素。

另请参阅 attribute (), setAttribute (),和 hasAttribute ().

QWebElement.removeAttributeNS ( self , QString  namespaceUri , QString  name )

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

另请参阅 attributeNS (), setAttributeNS (),和 hasAttributeNS ().

QWebElement.removeClass ( self , QString  name )

Removes the specified class with the given name 从 元素。

QWebElement.removeFromDocument ( self )

Removes this element from the document and makes it a null 元素。

另请参阅 removeAllChildren () 和 takeFromDocument ().

QWebElement.render ( self , QPainter   painter )

Render the element into painter .

QWebElement.render ( self , QPainter   painter , QRect   clip )

Render the element into painter clipping to clip .

QWebElement.replace ( self , QString  markup )

Replaces this element with the result of parsing markup .

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

另请参阅 encloseWith ().

QWebElement.replace ( self , QWebElement   element )

Replaces this element with element .

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

另请参阅 encloseWith ().

QWebElement.setAttribute ( self , QString  name , QString  value )

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

另请参阅 attribute (), attributeNS (),和 setAttributeNS ().

QWebElement.setAttributeNS ( self , QString  namespaceUri , QString  name , QString  value )

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 .

另请参阅 attributeNS (), attribute (),和 setAttribute ().

QWebElement.setFocus ( self )

Gives keyboard input focus to this element

另请参阅 hasFocus ().

QWebElement.setInnerXml ( self , QString  markup )

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

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

另请参阅 toInnerXml (), toOuterXml (),和 setOuterXml ().

QWebElement.setOuterXml ( self , QString  markup )

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.

注意: This is currently only implemented for (X)HTML 元素。

另请参阅 toOuterXml (), toInnerXml (),和 setInnerXml ().

QWebElement.setPlainText ( self , QString  text )

Replaces the existing content of this element with text .

This is equivalent to setting the HTML innerText property.

另请参阅 toPlainText ().

QWebElement.setStyleProperty ( self , QString  name , QString  value )

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.

另请参阅 styleProperty ().

QString QWebElement.styleProperty ( self , QString  name , StyleResolveStrategy   strategy )

Returns the value of the style with the given name 使用 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.

另请参阅 setStyleProperty ().

QString QWebElement.tagName ( self )

Returns the tag name of this element.

另请参阅 geometry ().

QWebElement QWebElement.takeFromDocument ( self )

Removes this element from the document and returns a reference 到它。

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

另请参阅 removeAllChildren () 和 removeFromDocument ().

QWebElement.toggleClass ( self , QString  name )

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 标签。

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

注意: The format of the markup returned will obey the namespace of the document containing the element. This means the return value will obey XML formatting rules, such as self-closing tags, only if the document is 'text/xhtml+xml'.

另请参阅 setInnerXml (), setOuterXml (),和 toOuterXml ().

QString QWebElement.toOuterXml ( self )

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

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

注意: The format of the markup returned will obey the namespace of the document containing the element. This means the return value will obey XML formatting rules, such as self-closing tags, only if the document is 'text/xhtml+xml'.

另请参阅 setOuterXml (), setInnerXml (),和 toInnerXml ().

QString QWebElement.toPlainText ( self )

Returns the text between the start and the end tag of this 元素。

This is equivalent to reading the HTML innerText property.

另请参阅 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   o )

bool QWebElement.__ne__ ( self , QWebElement   o )