使用 Selenium 的 text 和 innerHTML 之间的区别 [英] Difference between text and innerHTML using Selenium

查看:56
本文介绍了使用 Selenium 的 text 和 innerHTML 之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 selenium 时获取 textinnerHTML 有什么区别.即使我们在特定元素下有文本,当我们执行 .text 时,我们也会得到空值.但是做 .get_attribute("innerHTML") 工作正常.

Whats the difference between getting text and innerHTML when using selenium. Even though we have text under particular element, when we perform .text we get empty values. But doing .get_attribute("innerHTML") works fine.

有人能指出两者之间的区别吗?当有人应该在 .text 上使用 '.get_attribute("innerHTML")' 时?

Can someone point out the difference between two? When someone should use '.get_attribute("innerHTML")' over .text?

推荐答案

首先,text 是一个 属性,而 innerHTML属性.从根本上说,属性属性之间存在一些差异.

To start with, text is a property where as innerHTML is an attribute. Fundamentally there are some differences between a property and an attribute.

get_attribute(innerHTML) 获取元素的 innerHTML.

此方法将首先尝试返回具有给定名称的属性的值.如果具有该名称的属性不存在,则返回具有相同名称的 attribute 的值.如果没有具有该名称的 attribute,则返回 None.

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.

被视为真值的值,即等于 truefalse,将作为布尔值返回.所有其他非None 值都作为字符串返回.对于不存在的属性或属性,返回 None.

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.

  • 参数:

innerHTML - Name of the attribute/property to retrieve.

  • 示例:

  • Example:

    # Extract the text of an element.
    my_text = target_element.get_attribute("innerHTML") 
    

  • text 获取元素的文本.

    • 定义:

    def text(self):
    """The text of the element."""
    return self._execute(Command.GET_ELEMENT_TEXT)['value']
    

  • 示例:

  • Example:

    # Extract the text of an element.
    my_text = target_element.text   
    

  • 听起来还是很相似?阅读下文...

    Still sounds similar? Read below ...

    当浏览器加载页面时,它解析 HTML 并从中生成 DOM 对象.对于元素节点,大多数标准 HTML 属性会自动成为 DOM 对象的属性.

    When the browser loads the page, it parses the HTML and generates DOM objects from it. For element nodes, most standard HTML attributes automatically become properties of DOM objects.

    例如,如果标签是:

    <body id="page">
    

    然后DOM对象有body.id="page".

    注意:属性-属性映射不是一对一的!

    Note: The attribute-property mapping is not one-to-one!

    <小时>

    HTML 属性

    在 HTML 中,标签可能有属性.当浏览器解析 HTML 以创建标签的 DOM 对象时,它会识别标准属性并从中创建 DOM 属性.


    HTML attributes

    In HTML, tags may have attributes. When the browser parses the HTML to create DOM objects for tags, it recognizes standard attributes and creates DOM properties from them.

    因此,当元素具有 id 或其他标准属性时,会创建相应的属性.但如果属性是非标准的,则不会发生这种情况.

    So when an element has id or another standard attribute, the corresponding property gets created. But that doesn’t happen if the attribute is non-standard.

    注意:一个元素的标准属性对于另一个元素可能是未知的.例如,type<的标准属性/code> 标签,但不适用于 标签.标准属性在相应元素类的规范中进行了描述.

    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. Standard attributes are described in the specification for the corresponding element class.

    因此,如果一个属性是非标准的,那么它就没有 DOM 属性.在这种情况下,可以使用以下方法访问所有属性:

    So, if an attribute is non-standard, there won’t be a DOM-property for it. In that case all attributes are accessible by using the following methods:

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

    读取非标准属性的示例:

    An example of reading a non-standard property:

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

    <小时>

    属性-属性同步

    当标准属性发生变化时,相应的属性会自动更新,并且(有一些例外)反之亦然.但也有例外,例如 input.value 只从 attribute -> 同步到 property,而不是同步.这个特性其实是派上用场的,因为用户可能会修改值,然后在它之后,如果我们想从HTML中恢复原始"值,它在属性中.


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

    根据属性和属性在python中当我们用someObject.someAttr 之类的东西引用对象的属性时,Python 使用几种特殊的方法来获取对象的someAttr 属性.在最简单的情况下,属性只是实例变量.

    As per Attributes and Properties in python when we reference an attribute of an object with something like someObject.someAttr, Python uses several special methods to get the someAttr attribute of the object. In the simplest case, attributes are simply instance variables.

    从更广泛的角度来看:

    • 属性 是出现在对象名称之后的名称.这是句法结构.例如,someObj.name.
    • 实例变量是对象内部 __dict__ 中的一项.
    • 属性引用的默认语义是提供对实例变量的访问.当我们提到 someObj.name 时,默认行为实际上是 someObj.__dict__['name']
    • An attribute is a name that appears after an object name. This is the syntactic construct. For example, someObj.name.
    • An instance variable is an item in the internal __dict__ of an object.
    • The default semantics of an attribute reference is to provide access to the instance variable. When we mention someObj.name, the default behavior is effectively someObj.__dict__['name']

    在 Python 中,我们可以使用内置的 绑定具有属性名称的 gettersetter(和 deleter)函数property() 函数或 @property 装饰器.当我们这样做时,对属性的每个引用都具有直接访问实例变量的语法,但它调用给定的方法函数.

    In Python we can bind getter, setter (and deleter) functions with an attribute name, using the built-in property() function or @property decorator. When we do this, each reference to an attribute has the syntax of direct access to an instance variable, but it invokes the given method function.

    这篇关于使用 Selenium 的 text 和 innerHTML 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆