Difference Between Text and Innerhtml Using Selenium

Why text, innerText, innerHTML return empty in selenium

you are using text() instead of .text

There is no such thing called get_attribute("innerText")

Update 1 :

el = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div[class^='SdTitle']"))).text
print(el)

Using python and Selenium to scrape the innerText within an HTML element?

views is the WebElement which on printing rightly prints:

(session="12e48df447f7df855a1ee596ba609a30", element="1027ec31-8cb8-4758-b4b0-82b85628ed6c")


Solution

To print the text 417 you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR and text attribute:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "p#totReqCountVal[class$='js-total-requests']"))).text)
  • Using XPATH and get_attribute("innerHTML"):

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//p[@id='totReqCountVal' and contains(@class, 'js-total-requests')]"))).get_attribute("innerHTML"))
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC

You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python



References

Link to useful documentation:

  • get_attribute() method Gets the given attribute or property of the element.
  • text attribute returns The text of the element.
  • Difference between text and innerHTML using Selenium

Verify text - innerHTML from element using Selenium webdriver with python

Presumably, there will be a single footer with a webpage, so essentially instead of find_elements* you need to use find_element* and you can use the following solution:

expected_footer = "© 2020 Sauce Labs. All Rights Reserved. Terms of Service | Privacy Policy"
if expected_footer in WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//footer/div"))).text :
print("text visible...")
else:
pass

Note: You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Difference between innerText, innerHTML and value?

Unlike innerText, though, innerHTML lets you work with HTML rich text and doesn't automatically encode and decode text. In other words, innerText retrieves and sets the content of the tag as plain text, whereas innerHTML retrieves and sets the content in HTML format.

Modify innerHTML using Selenium

Try this:

WebElement element = ...
((JavascriptExecutor)driver).executeScript(
"var ele=arguments[0]; ele.innerHTML = 'my new content';", element);


Related Topics



Leave a reply



Submit