require 'rubygems'
require "watir"
browser = Watir::Browser.new :chrome,:switches => %w[--disable-notifications]
i=0;
begin
#TODO logining to the facebook
browser.goto 'linkedin.com'
browser.text_field(:id => 'login-email').set '[email protected]'
browser.text_field(:id => 'login-password').set 'ru09ec32'
browser.button(type: 'submit').click
puts '**** linkedin logged in ****'
sea = browser.div(:class => 'nav-search-bar')
puts sea
if sea.exits?
browser.div(:class => 'nav-search-bar').click
browser.div(:class => 'type-ahead-input-container').text_field.set 'wipro'
browser.button(:class => 'nav-search-button').click
puts '**** searched, the results for wipro is shown '
else
puts 'div not present'
end
side = browser.div(:class => 'right-rail search-right-rail')
puts side
if side.present?
puts 'div present'
side1 = browser.divs(:class => 'right-rail search-right-rail')
else
puts 'div not present'
end
#TODO exception handling
rescue Exception => e
p "Error Found..... #{e.message}"
end
The error is thrown while executing the above simply, I am able to found the div in real time but I am unable to check the div using if commands, I have also tried some codes like exists.
First of all, this line if sea.exits?
is wrong, change this to if sea.exists?
And your code is perfect, but very minute changes require to effectuate the WATIR's predefined wait statement.
You used this statement
side = browser.div(:class => 'right-rail search-right-rail')
If you write this way, then WATIR's predefined wait doesn't work because the method which you are going to call upon Watir::Div
object is where four checking happens,
for an example, if you call
browser.div(:class => 'right-rail search-right-rail').click
then these four following checking will happen
1)exist?
2)enabled?
3)present?
4)writable?
But if you don't call any function as I have shown then it will NOT return the element because statement is getting executed even before element is loaded, So as a result of that side.present? is false and it gives you the result that div not present
So change your code in the following way
browser.div(:class => 'right-rail search-right-rail').click
side = browser.div(:class => 'right-rail search-right-rail')
puts side
if side.present?
puts 'div present'
side1 = browser.divs(:class => 'right-rail search-right-rail')
else
puts 'div not present'
end
Now your code will work perfectly. present method will return true for you.