Using right-click to open a new tab in Watir

435 views
Skip to first unread message

Greg Allen

unread,
Jan 22, 2015, 12:36:15 PM1/22/15
to watir-...@googlegroups.com
I am using the following code to right-click on a link and open the target in a new tab in FireFox.

profile = Selenium::WebDriver::Firefox::Profile.new
profile.native_events = false

caps = Selenium::WebDriver::Remote::Capabilities.firefox(:native_events => false)
driver = Selenium::WebDriver.for(:firefox, profile: profile, desired_capabilities: caps)
agent = Watir::Browser.new(driver)

link = agent.a(:class => 'gb_ib')
agent.driver.action.context_click(link.wd).send_keys(:arrow_down).send_keys(:return).perform
puts agent.windows.count
puts agent.windows.last.title
agent.windows.last.use

It works fine, except that Watir does not seem to recognize the newly opened tab. The new tab gets opened, and I would expect the "agent.window.last.use" command to change the focus to that new tab. However, it does not.

In fact, printing out the number of windows results in 1 and the title of that page is "Google".

How can I make Watir recognize that I have opened a new tab in this manner?

Thanks!

Titus Fortner

unread,
Jan 22, 2015, 2:05:53 PM1/22/15
to watir-general
I suspect it is a timing issue, since it is taking the 2nd window a while to load, and watir won't recognize it until it is done.
Try:
agent.window(index: 1).when_present.use



--
--
Before posting, please read http://watir.com/support. In short: search before you ask, be nice.
 
watir-...@googlegroups.com
http://groups.google.com/group/watir-general
watir-genera...@googlegroups.com

---
You received this message because you are subscribed to the Google Groups "Watir General" group.
To unsubscribe from this group and stop receiving emails from it, send an email to watir-genera...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Greg Allen

unread,
Jan 22, 2015, 2:19:10 PM1/22/15
to watir-...@googlegroups.com
That's a good thought, but it still doesn't work.  I even updated the timeout to 60 seconds:

$ ruby test.rb
c:/Progra~2/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.10/lib/watir-webdriver/wait.rb:48:in `until': timed out after 60 seconds, waiting for {:index=>1} to become present
 (Watir::Wait::TimeoutError)
        from c:/Progra~2/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.10/lib/watir-webdriver/wait.rb:120:in `method_missing'
        from test.rb:22:in `<main>'

Titus Fortner

unread,
Jan 22, 2015, 2:32:37 PM1/22/15
to watir-general
Oh, this is a Firefox thing:

You have to open it in a new window:
agent.driver.action.context_click(link.wd).send_keys(:arrow_down).send_keys(:arrow_down).send_keys(:return).perform


Reply all
Reply to author
Forward
0 new messages