Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

python - Difference between text and innerHTML using Selenium

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.

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

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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")

get_attribute(innerHTML) gets the innerHTML 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:

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

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

text

text gets the text of the element.

  • Definition:

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


Attributes and properties

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.

For instance, if the tag is:

<body id="page">

then the DOM object has body.id="page".

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


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.

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.

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.

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): 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>

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.


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.

Python Attributes

In a broader perspective:

  • 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 Properties

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...