简体   繁体   English

在Selenium for Python中,如何获取元素的属性而不是其属性?

[英]In Selenium for Python, how can I get an element's attribute rather than its property?

According to the docs, get_attribute actually returns the property rather than the attribute, unless the property doesn't exist, in which case it falls back to the attribute. 根据文档,除非属性不存在,否则get_attribute实际上会返回属性而不是属性,在这种情况下,属性会退回到属性。

get_property will always return the property. get_property将始终返回该属性。

Is there a way to always get the attribute? 有没有办法总是获取属性? I find it weird that a function named "get_attribute" would prioritize the property value over the attribute value. 我觉得很奇怪,一个名为“ get_attribute”的函数会优先考虑属性值而不是属性值。

get_attribute(attribute_name) get_attribute(属性名称)

get_attribute(attribute_name) gets the given attribute or property of the element. get_attribute(attribute_name)获取元素的给定attributeproperty

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. 如果不存在具有该名称的属性,则它将返回具有相同名称的attribute的值。 If there's no attribute with that name, None is returned. 如果没有具有该名称的attribute ,则返回None

Values which are considered truthy, that is equals true or false , are returned as booleans. 视为真值的值,即truefalse ,将作为布尔值返回。 All other non- None values are returned as strings. 所有其他非None值作为字符串返回。 For attributes or properties which do not exist, None is returned. 对于不存在的属性,不返回None值。

  • Args: ARGS:

     attribute_name - Name of the attribute/property to retrieve. 
  • Example: 例:

     # Check if the "active" CSS class is applied to an element. is_active = "active" in target_element.get_attribute("class") 

get_property(property_name) get_property(PROPERTY_NAME)

get_property(property_name) gets the given property of the element. get_property(property_name)获取元素的给定属性。

  • Args: ARGS:

     property_name - Name of the property to retrieve. 
  • Example: 例:

     text_length = target_element.get_property("text_length") 

Still sounds similar? 听起来还是一样吗? Read below ... 参见下文 ...


Attributes and properties 属性和属性

When the browser loads the page, it parses the HTML and generates DOM objects from it. 当浏览器加载页面时,它会解析 HTML并从中生成DOM对象。 For element nodes, most standard HTML attributes automatically become properties of DOM objects. 对于元素节点,大多数标准HTML属性会自动成为DOM对象的属性。

For instance, if the tag is: 例如,如果标记为:

<body id="page">

then the DOM object has body.id="page" . 那么DOM对象具有body.id="page"

Note : The attribute-property mapping is not one-to-one! 注意 :属性-属性映射不是一对一的!


HTML attributes HTML属性

In HTML, tags may have attributes. 在HTML中,标记可能具有属性。 When the browser parses the HTML to create DOM objects for tags, it recognizes standard attributes and creates DOM properties from them. 当浏览器解析HTML以创建标签的DOM对象时,它会识别标准属性并从中创建DOM属性。

So when an element has id or another standard attribute, the corresponding property gets created. 因此,当一个元素具有id或另一个标准属性时,就会创建相应的属性。 But that doesn't happen if the attribute is non-standard. 但是,如果属性为非标准,则不会发生这种情况。

Note : A standard attribute for one element can be unknown for another one. 注意 :一个元素的标准属性对于另一个元素可能是未知的。 For instance, type is standard attribute for <input> tag, but not for <body> tag. 例如, type<input>标签的标准属性,而不是<body>标签的标准属性。 Standard attributes are described in the specification for the corresponding element class. 规范中对相应元素类描述了标准属性。

So, if an attribute is non-standard, there won't be a DOM-property for it. 因此,如果属性是非标准属性,则不会有DOM属性。 In that case all attributes are accessible by using the following methods: 在这种情况下,可以使用以下方法访问所有属性:

  • elem.hasAttribute(name) : checks for existence. elem.hasAttribute(name) :检查是否存在。
  • elem.getAttribute(name) : gets the value. elem.getAttribute(name) :获取值。
  • elem.setAttribute(name, value) : sets the value. elem.setAttribute(name, value) :设置值。
  • elem.removeAttribute(name) : removes the attribute. elem.removeAttribute(name) :删除属性。

An example of reading a non-standard property: 读取非标准属性的示例:

<body something="non-standard">
  <script>
    alert(document.body.getAttribute('something')); // non-standard
  </script>
</body>

Property-attribute synchronization 属性-属性同步

When a standard attribute changes, the corresponding property is auto-updated, and (with some exceptions) vice versa. 当标准属性更改时,相应的属性将自动更新,并且(除某些例外)反之亦然。 But there are exclusions, for instance input.value synchronizes only from attribute -> to property , but not back. 但是有一些例外情况,例如input.value仅从attribute >同步到propertyinput.value同步。 This feature actually comes in handy, because the user may modify value, and then after it, if we want to recover the "original" value from HTML, it's in the attribute. 功能实际上派上用场,因为用户可以修改值,然后在该值之后,如果我们想从HTML中恢复“原始”值,它就位于属性中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将硒与python结合使用,如何从JS中声明的HTML中获取Var <script> element - Using selenium with python, how can I get Var from HTML where it's declared in a JS <script> element Python Selenium Webdriver:如何使用get_attribute选择多个元素 - Python Selenium Webdriver: how to select more than one element using get_attribute Python | 硒| 继续输出页面源,而不是我手动检查元素时看到的内容 - Python | Selenium | Keeps outputting page source rather than what I see when I manually inspect element Selenium python查找元素并打印其属性 - Selenium python to locate an element and print its attribute 如何使用 selenium python 定位元素并获取编号 - how can i locate element and get the number by use selenium python 如何在 python 中的 selenium 中获取此特定元素 - How can I get this SPECIFIC element in selenium in python 如何在 Python 中使用 Selenium 来获取 JavaScript 元素? - How can I use Selenium in Python to get JavaScript element? 我何时以及为什么可以将描述符 class 的实例分配给 Python 中的 class 属性而不是使用属性? - When and why might I assign an instance of a descriptor class to a class attribute in Python rather than use a property? 如何让Python生成器返回None而不是StopIteration? - How can I get a Python generator to return None rather than StopIteration? 如何让我的 vscode 终端将我的 venv 用于 python 而不是系统版本? - How can I get my vscode terminal to use my venv for python rather than the system version?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM