1

I am trying to run a script where I open a web page and look for a specific element. If the element is present I would like to put 'Element Found', if the element is not present put 'Not Found'. I always get the error 'Unable to locate element'

I've tried to follow the advice here but it is still not working. I think this is something relatively easy, but I really don't get what I'm doing wrong:

require 'rubygems'
require 'selenium-webdriver'

driver = Selenium::WebDriver.for:firefox
driver.manage.timeouts.implicit_wait = 5
driver.get 'http://www.example.com'

case driver.find_element(:class_name,'example').displayed?
when true
    puts 'ELEMENT FOUND'
when false
    puts 'NOT FOUND'
end

with if statement:

#the rest is identical as in the above example
if (driver.find_element(:class_name,'example').displayed?)
    puts 'ELEMENT FOUND'
else
    puts 'NOT FOUND'
end

1 Answer 1

3

Your element cannot be found, so .displayed cannot be determined. .displayed can only be called on an element that exists and has already been found.

If you want to check existence of element, you should try as below :-

if (driver.find_elements(:class_name,'example').size > 0)
puts 'ELEMENT FOUND'
else
puts 'NOT FOUND'
end

Hope it will help you...:)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! it's exactly what i was looking for!
@Acolli you are welcome..please have a look meta.stackexchange.com/questions/5234/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.