30

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);
Jivings
  • 22,834
  • 6
  • 60
  • 101
grv
  • 1,013
  • 2
  • 12
  • 19
  • Context is a wonderful thing here - what do you mean it doesn't work? How does it not work? What does it do? Post a copy of your HTML, just around the element you are trying to get. Why you are scanning the ENTIRE page source for that? You may need to go back to the drawing board on Selenium tutorials. – Arran Jun 11 '12 at 21:13

3 Answers3

43

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
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
ChickenFeet
  • 2,653
  • 22
  • 26
  • 1
    Minor correction: the pytest one can (should) even be as simple as `assert text in driver.page_source` – Chris L. Barnes Aug 14 '18 at 21:02
  • 1
    Thanks for the feedback. I've modified my answer. – ChickenFeet Aug 15 '18 at 00:54
  • Does this mean the text is "visible" or simply that it exists somewhere in the source, but perhaps hidden by css or dropdown etc? – Jeppe Jan 15 '19 at 14:54
  • 1
    @Jeppe it means it is just in the HTML source - so yes, it could be hidden via CSS. This issue should be considered and the solution should be tested on a page to page basis. – ChickenFeet Jan 16 '19 at 04:28
  • @ChickenFeet Thanks. "Boon kwee"s answer relies on WebDriverWait to wait for the element to be clickable - so it must be fully visible. Other wait-conditions available here: https://selenium-python.readthedocs.io/waits.html – Jeppe Jan 18 '19 at 09:59
  • Being clickable may not mean it is visible. Having in the docs I see the method `visibility_of_element_located`, which might be a more suitable method to use. If you're waiting for an element to appear, please refer to [my answer here](https://stackoverflow.com/a/40903207/5387193). – ChickenFeet Jan 19 '19 at 10:09
  • How to detect for case insensitive comparison? – swdev Jun 18 '20 at 22:30
  • @swdev convert both search text and source text to the same case (e.g. lowercase). – ChickenFeet Jun 19 '20 at 03:10
  • @ChickenFeet I am thinking of doing that on a web page will be time consuming. I eventually using `re` like this: `if not re.search(text_to_be_found, driver.page_source, re.IGNORECASE)` Works for me – swdev Jun 19 '20 at 05:32
  • `assert "Tracking Details" in self.driver.page_source` this will not work for me. any new suggestion? – Krupal Vaghasiya Dec 30 '21 at 12:15
28

You can use driver.page_source and a simple regular expression to check if the text exists:

import re    
src = driver.page_source
text_found = re.search(r'text_to_search', src)
self.assertNotEqual(text_found, None)
limlim
  • 3,115
  • 2
  • 34
  • 46
4

You can try something like

browser = webdriver.Firefox()
browser.get(url)
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, 'some link text')))

Essentially the above lines launch Firefox, navigate to the specified url, cause the browser to hold for 10 seconds, for some url to load then look for a specific link text, if no link text is found, a TimeoutException is triggered.

Please note the number of brackets used, you will run into errors if the number of brackets does not correspond like the above.

To be able to run the above statement, the following must have been declared

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

This uses "element_to_be_clickable" - a full list of wait-conditions can be found here: Selenium Python: Waits

Jeppe
  • 1,830
  • 3
  • 24
  • 33
boon kwee
  • 119
  • 1
  • 4