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

13,007

Solution 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).

Solution 2

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'

Solution 3

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

Solution 4

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.

Solution 5

this worked for me...

driver.page_source.should_not include 'Login failed'
Share:
13,007
Curtis Miller
Author by

Curtis Miller

Senior Automation Engineer in Test Tools: Selenium Cypress Appium Languages: Java JavaScript Python Ruby C# / .Net

Updated on June 04, 2022

Comments

  • Curtis Miller
    Curtis Miller almost 2 years

    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.