Selenium WebElement中'property'和'attribute'之间有什么区别?

时间:2017-04-26 07:12:28

标签: selenium

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

他们在地球上有什么不同?

2 个答案:

答案 0 :(得分:7)

属性是给定DOM节点的静态属性,其中属性是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

答案 1 :(得分:0)

似乎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.
相关问题