【问题标题】:what is the difference between 'property' and 'attribute' in Selenium WebElement?Selenium WebElement 中的“属性”和“属性”有什么区别?
【发布时间】:2017-04-26 07:12:28
【问题描述】:

Selenium WebElement 有 2 个方法,在 Python 中,它们是 'get_attribute' 和 'get_property' 。文档对我来说非常简单且不清楚。

它们在地球上有什么区别?

【问题讨论】:

  • 它们不是特定于硒的术语。链接问题的answer 解释了差异。
  • get_property() 的最小文档也使用虚假属性text_length 是我困惑的根源。如果他们使用text_length = len(target_element.get_property("innerText")),会更清楚。

标签: selenium


【解决方案1】:

attribute 是给定 DOM 节点的静态属性,其中 property 是 DOM 节点对象的计算属性。 属性 的一个示例是复选框的checked 状态,或value 或输入字段。就像 属性 将是锚标记的 href 或输入 DOM 的 type

<a href="https://google.com" id="hello">Hello World</a>
<input type="checkbox" id="foo" checked>
<input type="text" id="bar" value="cheesecake">

link_location = document.querySelector('#hello').getAttribute('href')
// # href="https://google.com"

input_checkbox = document.querySelector('#foo').getAttribute('type')
// # type="checkbox"

checkbox_checked = document.querySelector('#foo').checked
// # computed property of the DOM node

textbox_value = document.querySelector('#bar').value
// # computed property of the DOM node

https://www.w3schools.com/jsref/dom_obj_all.asp

【讨论】:

    【解决方案2】:

    似乎get_attribute 搜索属性然后 属性和get_property 只是 搜索属性。

    来自代码documentation

    get_property

     def get_property(self, name):
            """
            Gets the given property of the element.
            :Args:
                - name - Name of the property to retrieve.
    

    get_attribute

     def get_attribute(self, name):
            """Gets the given attribute or property of the element.
            This method will first try to return the value of a property with the
            given name. If a property with that name doesn't exist, it returns the
            value of the attribute with the same name. If there's no attribute with
            that name, ``None`` is returned.
            Values which are considered truthy, that is equals "true" or "false",
            are returned as booleans.  All other non-``None`` values are returned
            as strings.  For attributes or properties which do not exist, ``None``
            is returned.
            :Args:
                - name - Name of the attribute/property to retrieve.
    

    【讨论】:

    • 这实际上并不能解释差异。它说明了方法的作用,但没有说明您可以期待什么结果。为此,您需要解释属性和属性之间的区别,这已经在其他地方进行了解释。
    • 这个答案特定于 Python(而不是 Selenium)。例如,在 Rust 的 thirty_four crate (= library) 中,get_attribute() 只能检索静态属性,而 get_property() 只能检索动态属性。
    猜你喜欢
    • 2011-11-14
    • 2016-04-29
    • 1970-01-01
    • 2010-09-20
    • 1970-01-01
    相关资源
    最近更新 更多