Search code examples
pythonpython-3.xselenium-webdriverassertion

How to assert text of the input in selenium webrdriver - python


I'm starting to learn automated testing and I don't know how to assert input text i selenium webrdiver in python. Could You help me?

This is my code - but it doesn't work properly

def assert_element_text(self, driver, xpath, expected_text):
        element = driver.find_element(by=By.XPATH, value=xpath)
        element_text = element.text
        assert expected_text == element_text
   
 def assert_surname(self):
        self.assert_element_text(self.driver, self.surname_field_xpath, self.expected_surname)

I have assertion error

I also use robot framework and 'Textfield Value Should Be' works in that case.


Solution

  • Try to get value of input element, by:

    element = driver.find_element(by=By.XPATH, value=xpath)
    element_text = element.get_attribute("value")
    assert expected_text == element_text