'How to get text from element using xpath

I want to write an assert to compare the expected text with the actual one. To do this, I need to pull out the text. Here is the code:

<div class="checkout-list">
  <div class="notification notification__inform " id="inform-not-product">
   <div class="message">
    <div class="text">
     "some test text"
    </div>
   </div>
  </div>
</div>

Here is my code:

WebDriverWait(browser, 5).until(
        EC.presence_of_element_located((By.XPATH, '//div[contains(@id, "inform-not-product")]//div[contains(@class, "text")]')))
information_message = browser.find_element(By.XPATH, '//div[contains(@id, "inform-not-product")]//div[contains(@class, "text")]').text()

I get an error:

Traceback (most recent call last): File "/Users/user/PycharmProjects/automation/tests/sales/1.py", line 130, in information_message = browser.find_element(By.XPATH, '//div[contains(@id, "inform-not-product")]//div[contains(@class, "text")]').text() TypeError: 'str' object is not callable

Also i tried to do the same with XPATH

WebDriverWait(browser, 5).until(
        EC.presence_of_element_located((By.XPATH, '//div[contains(@id, "inform-not-product")]//div[contains(@class, "text")]/text()[1]')))
information_message = browser.find_element(By.XPATH, '//div[contains(@id, "inform-not-product")]//div[contains(@class, "text")]/text()[1]').text()

and i get another error:

selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: The result of the xpath expression "//div[contains(@id, "inform-not-product")]//div[contains(@class, "text")]/text()[1]" is: [object Text]. It should be an element.

Pls help(



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source