4

I am trying to check to see if text is present using Selenium 2 and Firefox but cant seem to find the method to use. I tried to use the method is_text_present which seems to be what everyone says work but will not work for me. I get the returned error:

NoMethodError: undefined method `is_text_present' for# Selenium::WebDriver::Driver:0x1017232e0 browser=:firefox

How do you check the page for text using Selenium 2 and Firefox?

When I tried this stack overflow option "Finding text in page with selenium 2" it did not work for me, I believe it doesn't work because I am using Ruby to do my test, not Java.

4
  • verifyTextPresent and verifyText don't work? Commented Oct 7, 2011 at 1:12
  • no, when I try to use either I get "NoMethodError: undefined method". Where in the documentation are you seeing these as valid methods? Commented Oct 7, 2011 at 16:46
  • release.seleniumhq.org/selenium-core/1.0/reference.html#actions Commented Oct 8, 2011 at 9:51
  • Sean: That link has great information but a lot of it isn't used in Selenium 2, only Selenium 1. I can't seem to find any information for Selenium 2 specifically Ruby methods. Commented Oct 9, 2011 at 5:27

5 Answers 5

7

If you don't know in which element should be your text, you can use :

driver.page_source.include? 'TEXT_TO_SEARCH' 

Otherwise you can use as Llelong and Thomp suggested :

driver.find_element(:id=>"ELEMENT_ID").text.include? 'TEXT_TO_SEARCH' driver.find_element(:class=>"ELEMENT_CLASS").text.include? 'TEXT_TO_SEARCH' 
Sign up to request clarification or add additional context in comments.

Comments

4

You can find the text in JAVA using the following code snippet... Try using the same for Ruby:- driver.getPageSource().contains("TEXTTOSEARCH");

1 Comment

this worked great for finding text in an td with no class, id,name,title etc. Java Selenium 2
1
require 'selenium-webdriver' br = Selenium::WebDriver.for :firefox br.get "http://cnn.com" br.find_element(:id=>"cnnMainPage") br.find_element(:id=>"cnnMainPage").text.include? "Mideast" 

I tested this in the irb, so you may run into timing problems (may have to wait for the element to be present first).

Comments

1

I'm not sure how to do it in Ruby, but you should be able to call the getPageSource() method, and check to see if that contains the string of text you're looking for.

If you can't find the text in the source, you probably want to identify the exact element that contains the text and call the getText() method on that element. For example, these are some common identifiers for elements:

driver.findElement(By.xpath("xpathstring")).getText() driver.findElement(By.className("className")).getText() driver.findElement(By.name("elementname")).getText() driver.findElement(By.id("idname")).getText() 

There are several more element identifiers, you'll have to consult the documentation if these don't work for you.

2 Comments

I tried this but couldn't get it to check against the text in the source. Any idea on what else I could try to check the text in the source.
Posted some additional solutions for if you can't find the text in the source, hope this helps :).
1

this worked for me...

driver.page_source.should_not include 'Login failed' 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.