# Find element test with webdriver

**URL:** <https://rubytalk.org/t/find-element-test-with-webdriver/66847>\
**Category:** ruby-talk\
**Created:** [5 December 2012 20:15 UTC](https://rubytalk.org/t/find-element-test-with-webdriver/66847 "2012-12-05T20:15:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [5 December 2012 20:15 UTC](https://rubytalk.org/t/find-element-test-with-webdriver/66847/1 "2012-12-05T20:15:46Z")

</div>

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

&nbsp;&nbsp;def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;@driver = Selenium::WebDriver.for :firefox  
&nbsp;&nbsp;&nbsp;&nbsp;@driver.get "[http://google.com](http://google.com)"  
&nbsp;&nbsp;end

&nbsp;&nbsp;def by\_id  
&nbsp;&nbsp;&nbsp;&nbsp;#By ID - driver.find\_element(:id,\<elementID\>)  
&nbsp;&nbsp;&nbsp;&nbsp;if @driver.find\_element(:id, "gbqfbax")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'Did find element with Id: gbqfba'  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'Did not find element with Id: gbqfba'  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

test = Find\_element.new  
test.by\_id

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [5 December 2012 22:13 UTC](https://rubytalk.org/t/find-element-test-with-webdriver/66847/2 "2012-12-05T22:13:28Z")

</div>

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/](http://watirmelon.com/2011/05/05/selenium-webdriver-vs-watir-webdriver-in-ruby/)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [10 December 2012 18:18 UTC](https://rubytalk.org/t/find-element-test-with-webdriver/66847/3 "2012-12-10T18:18:41Z")

</div>

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?

&nbsp;&nbsp;def by\_id\_array  
&nbsp;&nbsp;&nbsp;&nbsp;by\_id = []  
&nbsp;&nbsp;&nbsp;&nbsp;by\_id \<\< @driver.find\_element(:id, "gbqfba")  
&nbsp;&nbsp;&nbsp;&nbsp;if by\_id.empty?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'Did find element with Id: gbqfba'  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'Did not find element with id gbqfba'  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

test = Find\_element.new  
test.by\_id\_array

--output--  
Did not find element with id gbqfba

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [10 December 2012 19:28 UTC](https://rubytalk.org/t/find-element-test-with-webdriver/66847/4 "2012-12-10T19:28:04Z")

</div>

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.com)'  
=\> "[http://www.google.co.uk/](http://www.google.co.uk/)"  
irb(main):004:0\> b.element(:id,'gbqfba').exists?  
=\> true

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [12 December 2012 19:51 UTC](https://rubytalk.org/t/find-element-test-with-webdriver/66847/5 "2012-12-12T19:51:18Z")

</div>

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

&nbsp;&nbsp;def by\_id\_array  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array = []  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array = @driver.find\_element(:id, "gbqfqX")  
&nbsp;&nbsp;&nbsp;&nbsp;puts 'No' if array.size == 0  
&nbsp;&nbsp;end

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Hassan\_Schroeder](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/hassan_schroeder/32/1838_2.png) [@Hassan\_Schroeder](https://rubytalk.org/u/Hassan_Schroeder)\
**Post date:** [12 December 2012 20:24 UTC](https://rubytalk.org/t/find-element-test-with-webdriver/66847/6 "2012-12-12T20:24:00Z")

</div>

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

> &nbsp;&nbsp;def by\_id\_array  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array =   
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array = @driver.find\_element(:id, "gbqfqX")  
> &nbsp;&nbsp;&nbsp;&nbsp;puts 'No' if array.size == 0  
> &nbsp;&nbsp;end

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

&nbsp;&nbsp;def by\_id  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@driver.find\_element(:id, 'gbqfbax')  
&nbsp;&nbsp;&nbsp;&nbsp;rescue =\> err  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'Did not find element with Id: gbqfba'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "because of #{err} (#{err.class})"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

HTH,

> **···**
>
> On Wed, Dec 12, 2012 at 11:51 AM, Mattias A. \<lists@ruby-forum.com\> wrote:  
> --  
> Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
> 
> > **[Hassan Schroeder on about.me](https://about.me/hassanschroeder)**
> >
> > I use about.me to show people what matters most to me.
> 
> twitter: @hassan

---

<div class="post-metadata">

**Author:** ![stomar](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@stomar](https://rubytalk.org/u/stomar)\
**Post date:** [12 December 2012 20:25 UTC](https://rubytalk.org/t/find-element-test-with-webdriver/66847/7 "2012-12-12T20:25:31Z")

</div>

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

> &nbsp;&nbsp;&nbsp;def by\_id\_array  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array =

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

> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array = @driver.find\_element(:id, "gbqfqX")  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts 'No' if array.size == 0  
> &nbsp;&nbsp;&nbsp;end

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

&nbsp;&nbsp;&nbsp;array = @driver.find\_elements(:id, "gbqfba")

(mind the plural).

> **···**
>
> Am 12.12.2012 20:51, schrieb Mattias A.:
> 
> --  
> \<[https://github.com/stomar/&gt](https://github.com/stomar/&gt);
