• PySide 模块
  • PySide.QtWebKit
  • 内容表

    上一话题

    QWebElementCollection

    下一话题

    QWebDatabase

    QWebElement

    注意

    该类在 Qt4.6 引入

    概要

    函数

    详细描述

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

    A PySide.QtWebKit.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 PySide.QtWebKit.QWebElement.findAll() and PySide.QtWebKit.QWebElement.findFirst() . These elements are identified using CSS selectors. The code snippet below demonstrates the use of PySide.QtWebKit.QWebElement.findAll() .

    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>
    allSpans = document.findAll("span")
    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 .

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

    Alternatively you can traverse the document manually using PySide.QtWebKit.QWebElement.firstChild() and PySide.QtWebKit.QWebElement.nextSibling() :

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

    Individual elements can be inspected or changed using methods such as PySide.QtWebKit.QWebElement.attribute() or PySide.QtWebKit.QWebElement.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:

    firstTextInput = document.findFirst("input[type=text]")
    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:

    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:

    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>
    button = document.findFirst("input[type=submit]")
    button.evaluateJavaScript("click()")
    										

    The underlying content of PySide.QtWebKit.QWebElement is explicitly shared. Creating a copy of a PySide.QtWebKit.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 PySide.QtWebKit.QWebElement.toPlainText() ; to XHTML using PySide.QtWebKit.QWebElement.toInnerXml() . To include the element's tag in the output, use PySide.QtWebKit.QWebElement.toOuterXml() .

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

    范例

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

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

    class PySide.QtWebKit. QWebElement
    class PySide.QtWebKit. QWebElement ( arg__1 )
    参数: arg__1 PySide.QtWebKit.QWebElement

    Constructs a null web element.

    构造副本为 other .

    PySide.QtWebKit.QWebElement. StyleResolveStrategy

    This enum describes how PySide.QtWebKit.QWebElement ‘s styleProperty resolves the given property name.

    常量 描述
    QWebElement.InlineStyle Return the property value as it is defined in the element, without respecting style inheritance and other CSS rules.
    QWebElement.CascadedStyle The property's value is determined using the inheritance and importance rules defined in the document's stylesheet.
    QWebElement.ComputedStyle The property's value is the absolute value of the style property resolved from the environment.
    PySide.QtWebKit.QWebElement. addClass ( name )
    参数: name – unicode

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

    PySide.QtWebKit.QWebElement. appendInside ( element )
    参数: element PySide.QtWebKit.QWebElement

    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.

    PySide.QtWebKit.QWebElement. appendInside ( markup )
    参数: markup – unicode

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

    Calling this function on a null element does nothing.

    PySide.QtWebKit.QWebElement. appendOutside ( markup )
    参数: markup – unicode

    Inserts the result of parsing markup after this element.

    Calling this function on a null element does nothing.

    PySide.QtWebKit.QWebElement. appendOutside ( element )
    参数: element PySide.QtWebKit.QWebElement

    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.

    PySide.QtWebKit.QWebElement. attribute ( name [ , defaultValue="" ] )
    参数:
    • name – unicode
    • defaultValue – unicode
    返回类型:

    unicode

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

    PySide.QtWebKit.QWebElement. attributeNS ( namespaceUri , name [ , defaultValue="" ] )
    参数:
    • namespaceUri – unicode
    • name – unicode
    • defaultValue – unicode
    返回类型:

    unicode

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

    PySide.QtWebKit.QWebElement. attributeNames ( [ namespaceUri="" ] )
    参数: namespaceUri – unicode
    返回类型: 字符串列表

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

    PySide.QtWebKit.QWebElement. ( )
    返回类型: 字符串列表

    Returns the list of classes of this element.

    PySide.QtWebKit.QWebElement. clone ( )
    返回类型: PySide.QtWebKit.QWebElement

    Returns a clone of this element.

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

    PySide.QtWebKit.QWebElement. document ( )
    返回类型: PySide.QtWebKit.QWebElement

    Returns the document which this element belongs to.

    PySide.QtWebKit.QWebElement. encloseContentsWith ( markup )
    参数: markup – unicode

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

    PySide.QtWebKit.QWebElement. encloseContentsWith ( element )
    参数: element PySide.QtWebKit.QWebElement

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

    ### illustration

    PySide.QtWebKit.QWebElement. encloseWith ( element )
    参数: element PySide.QtWebKit.QWebElement

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

    PySide.QtWebKit.QWebElement. encloseWith ( markup )
    参数: markup – unicode

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

    PySide.QtWebKit.QWebElement. evaluateJavaScript ( scriptSource )
    参数: scriptSource – unicode
    返回类型: object

    Executes scriptSource with this element as this 对象。

    PySide.QtWebKit.QWebElement. findAll ( selectorQuery )
    参数: selectorQuery – unicode
    返回类型: PySide.QtWebKit.QWebElementCollection

    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.

    PySide.QtWebKit.QWebElement. findFirst ( selectorQuery )
    参数: selectorQuery – unicode
    返回类型: PySide.QtWebKit.QWebElement

    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.

    PySide.QtWebKit.QWebElement. firstChild ( )
    返回类型: PySide.QtWebKit.QWebElement

    Returns the element's first child.

    PySide.QtWebKit.QWebElement. geometry ( )
    返回类型: PySide.QtCore.QRect

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

    PySide.QtWebKit.QWebElement. hasAttribute ( name )
    参数: name – unicode
    返回类型: PySide.QtCore.bool

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

    PySide.QtWebKit.QWebElement. hasAttributeNS ( namespaceUri , name )
    参数:
    • namespaceUri – unicode
    • name – unicode
    返回类型:

    PySide.QtCore.bool

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

    PySide.QtWebKit.QWebElement. hasAttributes ( )
    返回类型: PySide.QtCore.bool

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

    PySide.QtWebKit.QWebElement. hasClass ( name )
    参数: name – unicode
    返回类型: PySide.QtCore.bool

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

    PySide.QtWebKit.QWebElement. hasFocus ( )
    返回类型: PySide.QtCore.bool

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

    PySide.QtWebKit.QWebElement. isNull ( )
    返回类型: PySide.QtCore.bool

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

    PySide.QtWebKit.QWebElement. lastChild ( )
    返回类型: PySide.QtWebKit.QWebElement

    Returns the element's last child.

    PySide.QtWebKit.QWebElement. localName ( )
    返回类型: unicode

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

    PySide.QtWebKit.QWebElement. namespaceUri ( )
    返回类型: unicode

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

    PySide.QtWebKit.QWebElement. nextSibling ( )
    返回类型: PySide.QtWebKit.QWebElement

    Returns the element's next sibling.

    PySide.QtWebKit.QWebElement. __ne__ ( o )
    参数: o PySide.QtWebKit.QWebElement
    返回类型: PySide.QtCore.bool

    Returns true if this element points to a different underlying DOM object than o ;否则返回 false。

    PySide.QtWebKit.QWebElement. __eq__ ( o )
    参数: o PySide.QtWebKit.QWebElement
    返回类型: PySide.QtCore.bool

    Returns true if this element points to the same underlying DOM object as o ;否则返回 false。

    PySide.QtWebKit.QWebElement. parent ( )
    返回类型: PySide.QtWebKit.QWebElement

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

    PySide.QtWebKit.QWebElement. prefix ( )
    返回类型: unicode

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

    PySide.QtWebKit.QWebElement. prependInside ( element )
    参数: element PySide.QtWebKit.QWebElement

    前置 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.

    PySide.QtWebKit.QWebElement. prependInside ( markup )
    参数: markup – unicode

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

    Calling this function on a null element does nothing.

    PySide.QtWebKit.QWebElement. prependOutside ( markup )
    参数: markup – unicode

    Inserts the result of parsing markup before this element.

    Calling this function on a null element does nothing.

    PySide.QtWebKit.QWebElement. prependOutside ( element )
    参数: element PySide.QtWebKit.QWebElement

    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.

    PySide.QtWebKit.QWebElement. previousSibling ( )
    返回类型: PySide.QtWebKit.QWebElement

    Returns the element's previous sibling.

    PySide.QtWebKit.QWebElement. removeAllChildren ( )

    Removes all children from this element.

    PySide.QtWebKit.QWebElement. removeAttribute ( name )
    参数: name – unicode

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

    PySide.QtWebKit.QWebElement. removeAttributeNS ( namespaceUri , name )
    参数:
    • namespaceUri – unicode
    • name – unicode

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

    PySide.QtWebKit.QWebElement. removeClass ( name )
    参数: name – unicode

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

    PySide.QtWebKit.QWebElement. removeFromDocument ( )

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

    PySide.QtWebKit.QWebElement. render ( painter )
    参数: painter PySide.QtGui.QPainter

    Render the element into painter .

    PySide.QtWebKit.QWebElement. render ( painter , clipRect )
    参数:

    Render the element into painter clipping to clip .

    PySide.QtWebKit.QWebElement. replace ( element )
    参数: element PySide.QtWebKit.QWebElement

    Replaces this element with element .

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

    PySide.QtWebKit.QWebElement. replace ( markup )
    参数: markup – unicode

    Replaces this element with the result of parsing markup .

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

    PySide.QtWebKit.QWebElement. setAttribute ( name , value )
    参数:
    • name – unicode
    • value – unicode

    Adds an attribute with the given name and value 。若存在同名属性,其值被替换由 value .

    PySide.QtWebKit.QWebElement. setAttributeNS ( namespaceUri , name , value )
    参数:
    • namespaceUri – unicode
    • name – unicode
    • value – unicode

    Adds an attribute with the given name in namespaceUri with value 。若存在同名属性,其值被替换由 value .

    PySide.QtWebKit.QWebElement. setFocus ( )

    Gives keyboard input focus to this element

    PySide.QtWebKit.QWebElement. setInnerXml ( markup )
    参数: markup – unicode

    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.

    注意

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

    PySide.QtWebKit.QWebElement. setOuterXml ( markup )
    参数: markup – unicode

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

    PySide.QtWebKit.QWebElement. setPlainText ( text )
    参数: text – unicode

    Replaces the existing content of this element with text .

    This is equivalent to setting the HTML innerText property.

    PySide.QtWebKit.QWebElement. setStyleProperty ( name , value )
    参数:
    • name – unicode
    • value – unicode

    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.

    PySide.QtWebKit.QWebElement. styleProperty ( name , strategy )
    参数:
    返回类型:

    unicode

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

    PySide.QtWebKit.QWebElement. tagName ( )
    返回类型: unicode

    Returns the tag name of this element.

    PySide.QtWebKit.QWebElement. takeFromDocument ( )
    返回类型: PySide.QtWebKit.QWebElement

    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.

    PySide.QtWebKit.QWebElement. toInnerXml ( )
    返回类型: unicode

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

    注意

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

    PySide.QtWebKit.QWebElement. toOuterXml ( )
    返回类型: unicode

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

    PySide.QtWebKit.QWebElement. toPlainText ( )
    返回类型: unicode

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

    This is equivalent to reading the HTML innerText property.

    PySide.QtWebKit.QWebElement. toggleClass ( name )
    参数: name – unicode

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

    PySide.QtWebKit.QWebElement. webFrame ( )
    返回类型: PySide.QtWebKit.QWebFrame

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