Find element test with webdriver

Hi, if my test not find the element i want it to continue and output
that it was not found. But below code fails, i want my test to output ’
Did not find element with id: gbgfba’. Thanks for your help in advance!
(element id should be gbqfba but have entered gbqfbax so it should fail)

require ‘selenium-webdriver’
class Find_element

def initialize
@driver = Selenium::WebDriver.for :firefox
@driver.gethttp://google.com
end

def by_id
#By ID - driver.find_element(:id,)
if @driver.find_element(:id, “gbqfbax”)
puts ‘Did find element with Id: gbqfba’
else
puts ‘Did not find element with Id: gbqfba’
end
end
end

test = Find_element.new
test.by_id

The best workaround I’ve seen for this was to use findelements to return
an array and check whether the size is 0.
Of course, you could always use Watir-Webdriver which is more versatile
and uses the same underlying driver with a better API.

http://watirmelon.com/2011/05/05/selenium-webdriver-vs-watir-webdriver-in-ruby/

Thanks for your reply. According to array you mean something like below?
But it output that the method not find the element but it should. Any
ide?

def by_id_array
by_id = []
by_id << @driver.find_element(:id, “gbqfba”)
if by_id.empty?
puts ‘Did find element with Id: gbqfba’
else
puts ‘Did not find element with id gbqfba’
end
end

test = Find_element.new
test.by_id_array

–output–
Did not find element with id gbqfba

Thanks for your answer Joel. I really want to solve this with Selenium
Webdriver even if watir is better. (cannot give up now :))

Your array tips dosent work when the element not exists or i maybe I do
something wrong? Expected output below would be “No”, but the test fails
and will not complete the run. It complains about the row array =
@driver.find_element(:id, “gbqfqX”)

def by_id_array
array = []
array = @driver.find_element(:id, “gbqfqX”)
puts ‘No’ if array.size == 0
end

I meant something like this:

array = @driver.find_elements(:id, “gbqfba”)
puts ‘Did not find element’ if array.size = 0

As for whether the element exists to be found you’ll need to ensure that
it’s actually the ID you need to identify it. I recommend using Firebug
as it will give you the names, IDs, even the full XPath of any element.
Use the Interactive Ruby console (IRB) to test out what works in
real-time.

For example, this is what I’d do in watir-webdriver:

irb(main):001:0> require ‘watir-webdriver’
=> true
irb(main):002:0> b = Watir::Browser.new
=> #<Watir::Browser:0x10e5b480 url=“about:blank” title="">
irb(main):003:0> b.goto ‘www.google.com
=> “http://www.google.co.uk/
irb(main):004:0> b.element(:id,‘gbqfba’).exists?
=> true

On Wed, Dec 12, 2012 at 11:51 AM, Mattias A. [email protected]
wrote:

Your array tips dosent work when the element not exists or i maybe I do
something wrong? Expected output below would be “No”, but the test fails
and will not complete the run. It complains about the row array =
@driver.find_element(:id, “gbqfqX”)

Specifying exactly what that “complaint” is would help people help
you, but …

def by_id_array
array = []
array = @driver.find_element(:id, “gbqfqX”)
puts ‘No’ if array.size == 0
end

It helps to pay attention to what’s actually returned by the method
you’re calling. Try using this:

def by_id
begin
@driver.find_element(:id, ‘gbqfbax’)
rescue => err
puts ‘Did not find element with Id: gbqfba’
puts “because of #{err} (#{err.class})”
end
end

HTH,

Am 12.12.2012 20:51, schrieb Mattias A.:

Thanks for your answer Joel. I really want to solve this with Selenium
Webdriver even if watir is better. (cannot give up now :))

Your array tips dosent work when the element not exists or i maybe I do
something wrong? Expected output below would be “No”, but the test fails
and will not complete the run. It complains about the row array =
@driver.find_element(:id, “gbqfqX”)

Be more specific (provide the error message).

def by_id_array
array = []

The previous line is useless, because `array’ is redefined
in the following line:

  array = @driver.find_element(:id, "gbqfqX")
 puts 'No' if array.size == 0

end

You need to read Joel’s answers more carefully, he suggested

array = @driver.find_elements(:id, “gbqfba”)

(mind the plural).

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs