Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if some text is present on a web page using selenium 2?

Tags:

Hi I am using selenium to automate test on web pages. I am using selenium 2 and python and would like to have answers in this framework only. SO how do I check whether some text is present or not? I have tried asset equals but it is not working?

assertEquals(driver.getPageSource().contains("email"), true); 
like image 356
grv Avatar asked Jun 11 '12 11:06

grv


People also ask

How do you check if an element is present in Selenium?

New Selenium IDE To check the presence of an element, we can use the method – findElements. The method findElements returns a list of matching elements. Then, we have to use the method size to get the number of items in the list. If the size is 0, it means that this element is absent from the page.

Is text present in Selenium?

text(): A built-in method in Selenium WebDriver that is used with XPath locator to locate an element based on its exact text value. contains(): Similar to the text() method, contains() is another built-in method used to locate an element based on partial text match.


1 Answers

For those of you who are still interested:

Generic Solution

if text in driver.page_source:     # text exists in page 

unittest:

assertTrue(text in driver.page_source) 

pytest:

assert text in driver.page_source 
like image 129
ChickenFeet Avatar answered Oct 09 '22 18:10

ChickenFeet