Selenium can't get the solution for label text

Selenium can't get the solution for the label text------ that is, the solution for driver.find_element_by_xxx().text() is empty

If the resulting text is only empty, instead of the baidu we expect, the currently positioned element may be hidden.

1. Determine whether it is hidden. driver.find_element_by_xx(). is_displayed()  

  Print the result, if you get a false result. That means it is hidden

2. How to solve it?

      Method 1: Modify the CSS of the currently positioned element on the page, or the parent element of the current element, so that the is_displayed()   value of the element is true.

      Method 2:   For elements whose is_displayed()   is false, the attributes of the element can still be obtained through the getAttribute() method. 

     By webdriver specdefinition, Selenium WebDriver will only interact with visible elements, so getting the text of a hidden element will always return an empty string. 
    However, in some cases, we need to get the text of hidden elements. These contents can be obtained using element.attribute('attributeName'), through textContentinnerTextinnerHTMLand other attributes . (emphasis added)

    • innerHTML Will return the inner HTML of the element, including all HTML tags. 
      For example, <div>Hello <p>World!</p></div>will innerHTMLgetHello <p>World!</p>
    • textContent and  innerText will only get text content, not HTML tags. 
      • textContent is a W3C compliant text content attribute, but not supported by IE
      • innerText Not specified by the W3C DOM, FireFox does not support

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------     

For example: locate 126 when the email is successfully sent, locate the text "successfully sent"

      

 text= driver.find_element_by_class_name('tK1') # Locate 

print(ss.get_attribute("innerHTML")) # Using innerHTML will return the inner HTML of the element, including all HTML tags.
  print(ss.get_attribute('textContent')) #Using              textContent and innerText will only get the text content without HTML tags.
                                
  print(ss.get_attribute('innerText'))                     innerText is not the specified content of W3C DOM, FireFox does not support
 Run the above 3 sentences with Google browser and print the results as follows

      1.<b class="nui-ico se0 pv1"></b>Successfully sent<span style="font-size: 14px; margin-left: 5px;"><a id="_mail_link_39_262" href="javascript :void(0)" class=" js-component-link nui-txt-link" hidefocus="hidefocus">Available to receive responses on mobile phones</a></span><span></span><span style=" font-size: 14px; margin-left: 5px;"><a id="_mail_link_40_264" href="javascript:void(0)" class=" js-component-link nui-txt-link" hidefocus="hidefocus" ><b class="nui-ico nui-ico-sms nui-ico-sms" style="margin-bottom: 10px;"></b>Free SMS notification</a></span>
    2. Successfully sent Receive free SMS notifications for replies on your mobile phone

    3. <empty>

    It can be seen that  innerText  does not support Google!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325499493&siteId=291194637