Selenium-webdriver + puts page title

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.gethttp://google.com
end

def by_id
#By ID - driver.find_element(:id,)
@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 `’

Process finished with exit code 1

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

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.gethttp://google.com
end

def by_id
#By ID - driver.find_element(:id,)
@driver.find_element (:id, “q”)
puts “Page title is #{@driver.title}”
end

def by_name
#By name - driver.find_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

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

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! :smiley:

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! :smiley:


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.gethttp://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,)
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,)
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

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?