I am attempting to validate that text is present on a page. Validating an element by ID is simple enough, buy trying to do it with text isn't working right. And, I can not locate the correct attribute for By to validate text on a webpage.
Example that works for ID using By attribute
self.assertTrue(self.is_element_present(By.ID, "FOO"))
Example I am trying to use (doesn't work) for text using By attribute
self.assertTrue(self.is_element_present(By.TEXT, "BAR"))
I've tried these as well, with *error (below)
self.assertTrue(self.is_text_present("FOO"))
and
self.assertTrue(self.driver.is_text_present("FOO"))
*error: AttributeError: 'WebDriver' object has no attribute 'is_element_present'
I have the same issue when trying to validate By.Image
as well.
From what I have seen, is_element_present is generated by a Firefox extension (Selenium IDE) and looks like:
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException: return False
return True
"By" is imported from selenium.webdriver.common:
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
There are several "By" constants to address each API find_element_by_* so, for example:
self.assertTrue(self.is_element_present(By.LINK_TEXT, "My link"))
verifies that a link exists and, if it doesn't, avoids an exception raised by selenium, thus allowing a proper unittest behaviour.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With