开发者

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

开发者 https://www.devze.com 2023-04-10 18:33 出处:网络
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 开发者_Go百科to be what every

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 开发者_Go百科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.


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'


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


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


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.


this worked for me...

driver.page_source.should_not include 'Login failed'
0

精彩评论

暂无评论...
验证码 换一张
取 消