Home > other >  Switching windows using Watir is timing out
Switching windows using Watir is timing out

Time:07-19

I want to automate work between two browser windows within the same Watir session. However, the code below times out before I'm able to see a new browser tab open.

b = = Watir::Browser.new :chrome
b.goto 'www.google.com'
b.switch_window

/Users/andyhuynh/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/watir-7.1.0/lib/watir/wait.rb:41:in `until': timed out after 30 seconds, waiting for true condition on #<Watir::Browser:0x135ba826800b0080 url="https://www.google.com/?gws_rd=ssl" title="Google"> (Watir::Wait::TimeoutError)
        from /Users/andyhuynh/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/watir-7.1.0/lib/watir/wait.rb:110:in `wait_until'
        from /Users/andyhuynh/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/watir-7.1.0/lib/watir/has_window.rb:63:in `switch_window'
        from /Users/andyhuynh/Code/ilounge_voting/ilounge_voting.rb:68:in `<top (required)>'
        from <internal:/Users/andyhuynh/.asdf/installs/ruby/3.0.0/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
        from <internal:/Users/andyhuynh/.asdf/installs/ruby/3.0.0/lib/ruby/3.0.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
        from /Users/andyhuynh/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/irb-1.4.1/lib/irb/init.rb:395:in `block in load_modules'
        from /Users/andyhuynh/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/irb-1.4.1/lib/irb/init.rb:393:in `each'
        from /Users/andyhuynh/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/irb-1.4.1/lib/irb/init.rb:393:in `load_modules'
        from /Users/andyhuynh/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/irb-1.4.1/lib/irb/init.rb:21:in `setup'
        from /Users/andyhuynh/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/irb-1.4.1/lib/irb.rb:412:in `start'
        from /Users/andyhuynh/.asdf/installs/ruby/3.0.0/lib/ruby/gems/3.0.0/gems/irb-1.4.1/exe/irb:11:in `<top (required)>'
        from /Users/andyhuynh/.asdf/installs/ruby/3.0.0/bin/irb:23:in `load'
        from /Users/andyhuynh/.asdf/installs/ruby/3.0.0/bin/irb:23:in `<main>'

I'm on Watir version 7.0.0, Ruby 3.0.0p0 and Chrome is Version 103.0.5060.114. My initial thought is it's either my Ruby or Chrome versions. I tried downgrading to different versions and did not amount to anything. I'm out of ideas and any leads are much appreciated. Thanks

CodePudding user response:

When you open b.goto 'www.google.com' control is already in that window so you don't have to switch, you should open another window to make your switch. I have used b.execute_script("window.open('https://spiritualgab.freeforums.net')") to open another window in the given below program.

Try this.

require 'watir'
b = Watir::Browser.new :chrome
b.goto 'www.google.com'
b.execute_script("window.open('https://spiritualgab.freeforums.net')")
b.window(title: 'Home | Spiritualgab').use
b.element(xpath: "//*[@id='navigation-menu']/ul/li[1]/a").click
b.original_window.use
b.text_field(name:'q').set 'raja'

By chance If you don't know the title of the window, just execute p b.title will print the title for you.

  • Related