Forum: Ruby selenium-webdriver + puts page title

Posted by Mattias A. (mattias_a)
on 2012-12-04 18:43
Hi,

Can anyone help me with why this fails? Thanks in advance.

require 'selenium-webdriver'
class Find_element

  def initialize
    @driver = Selenium::WebDriver.for :firefox
    @driver.get "http://google.com"
  end

  def by_id
    #By ID - driver.find_element(:id,<elementID>)
    @driver.find_element(:id, "q")
    puts "Page title is #{@driver.title}"
  end

test = Find_element.new
test.by_id
end

from lib/findElementMethod.rb:11:in `by_id'
from lib/findElementMethod.rb:16:in `<class:Find_element>'
from lib/findElementMethod.rb:2:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'

Process finished with exit code 1
Posted by unknown (Guest)
on 2012-12-04 19:51
(Received via mailing list)
Am 04.12.2012 18:43, schrieb Mattias A.:
>      @driver.find_element(:id, "q")
>      puts "Page title is #{@driver.title}"
>    end
>
> test = Find_element.new
> test.by_id
> end

First, you have a misplaced `end' (the last one).

I cannot reproduce your error messages, but for me the
code results in a TimeOut error, because there is no element
with id='q'. Changing :id to :name works fine (or try with
an id that actually exists on the page).
Posted by Mattias A. (mattias_a)
on 2012-12-04 20:19
unknown wrote in post #1087821:
> Am 04.12.2012 18:43, schrieb Mattias A.:
>>      @driver.find_element(:id, "q")
>>      puts "Page title is #{@driver.title}"
>>    end
>>
>> test = Find_element.new
>> test.by_id
>> end
>
> First, you have a misplaced `end' (the last one).
>
> I cannot reproduce your error messages, but for me the
> code results in a TimeOut error, because there is no element
> with id='q'. Changing :id to :name works fine (or try with
> an id that actually exists on the page).

Thanks for your reply, so it should be as below?

...
    puts "Page title is #{@driver.title}"
  end
end
test = Find_element.new
test.by_id

-------------------
I did a new try with below but it insists on running the method by_id 
even if i not call it? Any ides?
-------------------
require 'selenium-webdriver'
class Find_element

  def initialize
    @driver = Selenium::WebDriver.for :firefox
    @driver.get "http://google.com"
  end

  def by_id
    #By ID - driver.find_element(:id,<elementID>)
    @driver.find_element (:id, "q")
    puts "Page title is #{@driver.title}"
  end

  def by_name
    #By name - driver.find_element(:name,<element name>)
    @driver.find_element (:name, "q")
    puts "Page title is #{@driver.title}"
  end

end

test = Find_element.new
#test.by_id
test.by_name
Posted by unknown (Guest)
on 2012-12-04 20:39
(Received via mailing list)
Am 04.12.2012 20:19, schrieb Mattias A.:
> -------------------
> I did a new try with below but it insists on running the method by_id
> even if i not call it? Any ides?

I do not think it does. Try including statements like
"puts 'method by_id'" and "puts 'method by_name'"
in the respective method.

>      #By ID - driver.find_element(:id,<elementID>)
>      @driver.find_element (:id, "q")

You should remove the space before the argument parentheses
(it causes an error in Ruby 1.9.3).

   @driver.find_element(:id, "q")
Posted by Mattias A. (mattias_a)
on 2012-12-04 22:29
unknown wrote in post #1087829:
> Am 04.12.2012 20:19, schrieb Mattias A.:
>> -------------------
>> I did a new try with below but it insists on running the method by_id
>> even if i not call it? Any ides?
>
> I do not think it does. Try including statements like
> "puts 'method by_id'" and "puts 'method by_name'"
> in the respective method.
When I removed the space in def by_id it passed, it dident run the code 
due to the syntax error in this method. (as i understands it)

>
>>      #By ID - driver.find_element(:id,<elementID>)
>>      @driver.find_element (:id, "q")
>
> You should remove the space before the argument parentheses
> (it causes an error in Ruby 1.9.3).
>
>    @driver.find_element(:id, "q")

Thanks for your help, now it works! :D
Posted by Mattias A. (mattias_a)
on 2012-12-05 05:58
Mattias A. wrote in post #1087838:
> unknown wrote in post #1087829:
>> Am 04.12.2012 20:19, schrieb Mattias A.:
>>> -------------------
>>> I did a new try with below but it insists on running the method by_id
>>> even if i not call it? Any ides?
>>
>> I do not think it does. Try including statements like
>> "puts 'method by_id'" and "puts 'method by_name'"
>> in the respective method.
> When I removed the space in def by_id it passed, it dident run the code
> due to the syntax error in this method. (as i understands it)
>
>>
>>>      #By ID - driver.find_element(:id,<elementID>)
>>>      @driver.find_element (:id, "q")
>>
>> You should remove the space before the argument parentheses
>> (it causes an error in Ruby 1.9.3).
>>
>>    @driver.find_element(:id, "q")
>
> Thanks for your help, now it works! :D
-----
Hi again, this is maybe a better way to do it? When you should test if 
the element are available or not? Or how should you solve it?

require 'selenium-webdriver'
class Find_element

  def initialize
    @driver = Selenium::WebDriver.for :firefox
    @driver.get "http://google.com"
  end

  def page_title
    if @driver.title
      puts "Page title found: #{@driver.title}"
    else
      puts "Page title not found"
    end

  end

  def by_id
    #By ID - driver.find_element(:id,<elementID>)
    if @driver.find_element(:id, "gbqfba")
      puts 'Did find element with Id: gbqfba'
    else
      puts 'Did not find element with id gbqfba'
    end
  end

  def by_name
    #By name - driver.find_element(:name,<element name>)
    if @driver.find_element(:name, "q")
      puts 'Did find element with Name: q'
    else
      puts 'Did not find element with Name: q'
    end
  end

end

test = Find_element.new
test.by_id
test.by_name
test.page_title
Posted by Mattias A. (mattias_a)
on 2012-12-05 06:11
Noticed that it dident worked so well when I entered an element that
dident exist.. How should i return that it not exists without resulting
code errors when i run the test?
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.