使用Watir切换窗口超时



我想在同一个Watir会话中自动完成两个浏览器窗口之间的工作。但是,在我能够看到一个新的浏览器选项卡打开之前,下面的代码超时了。

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

我在Watir版本7.0.0,Ruby 3.0.0和Chrome是版本103.0.5060.114。我最初的想法是它要么是我的Ruby或Chrome版本。我试着降级到不同的版本,但没有任何效果。我已经无计可施了,任何建议都非常感谢。由于

当您打开b.goto 'www.google.com'控件已经在该窗口中,因此您不必切换,您应该打开另一个窗口进行切换。我使用b.execute_script("window.open('https://spiritualgab.freeforums.net')")在下面给出的程序中打开另一个窗口。

试试这个。

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如果你不知道窗口的标题,只需执行p b.title就会为你打印标题。

最新更新