0

Selenium: test if element contains some text

It is possible to do in selenium IDE but I don't know how to do it with python and selenium.
I want to set a waiting that wait until that element contains part of the specified text. Thanks.

Prophet
  • 32,350
  • 22
  • 54
  • 79

2 Answers2

0

You can wait for element like this :

from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
element = wait.until(EC.visibility_of_element_located((By.ID, 'someid')))

extract the text like this :

actual_text = element.text

check whether it contains expected text or not like this:

self.assertIn('expected_string_here', actual_text) 

or like this :

print expected_string_here == actual_text
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
0

Yes, you can use this:

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


WebDriverWait(self.driver, 30).until(
        EC.text_to_be_present_in_element(By.ID, 'your_element_id', "The_text_you_are_looking_for"))

The element can be located by class name, css_selector, xpath etc.
The By.ID and the element locator should be updated accordingly

Prophet
  • 32,350
  • 22
  • 54
  • 79
  • hi thanks for your reply but this method seems only work for the expected text exactly equal to the element text. But my question is: if the element text is "abcde", can I use "wait" to see whether the element text contains "abc" – Horus Yeung May 28 '21 at 09:16
  • Did you try this? This method should match condition of text in (contains) that web element, not just equals. – Prophet May 28 '21 at 09:19