0

How can I check if text is present in selenium webdriver & python?

I tried :

def get_email(browser):

email_check = driver.find_element(By.CLASS_NAME("viewed-user-email")).text

assert user_name in email_check, "Text not found %s" % user_name

the response is:

 TypeError: 'str' object is not callable
chani.k
  • 11
  • 2
  • 4

1 Answers1

0

If you are looking for how to check whether a string is substring of another string, here is the answer:

assert my_email_address in email_check, "Text not found %s" % my_email_address
  1. Use in keyword for contains
  2. Use message at the end of assert statement, not in beginning.

Complete code:

def get_email(browser): 
    email_check = browser.findElement(By.CLASS_NAME("viewed-user-email")).text
    assert my_email_address in email_check, "Text not found %s" % my_email_address

References:

  1. What is the use of "assert" in Python?
  2. Does Python have a string contains substring method?
Community
  • 1
  • 1
Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65