ClubSlice
ClubSlice

Reputation: 41

How to access a new browser tab

Watir noob here. I'm attempting to connect to and manipulate a newly spawned browser tab, but am a little confused about how to recognize the new tab. Here's what I have:

Windows 7 (32 bit) IE 10

Ruby 1.9.3-p392/TDM-32.4.5.2

LOCAL GEMS

watir (4.0.2 x86-mingw32)

watir-classic (3.6.0)

watir-webdriver (0.6.3)

Basic gist of the Script:

require 'rubygems'
require 'Watir'
ie=Watir::Browser.new

... First part of the script on the original browser tab is completed. The original browser tab generates a second browser tab, which I need to access. I've read a response somewhere that I need to link to the browser tab prior to attaching to it, but I'm having a hard time recognizing the link by the href, url or title: (where 'path...' = http url)

ie.link(:href, "path...").click

--Uncaught exception: Unable to locate element, using {:tag_name=>["a"], :href=>"path..."

ie.link(:url, "path...").click

--Uncaught exception: Unable to locate element, using {:tag_name=>["a"], :url=>"path..."

ie.a(:href, "path...").click

--Unable to locate element, using {:tag_name=>["a"], :url=>"path..."}

However, if I try the following:

ie2 = Watir::IE.attach(:title, 'New_Tab_Title')

--"Unable to locate a window with title of New_Tab_Title"

Firstly, how do I point to the new url or browser tab? If I do a ie.close after the second tab opens, the first tab closes, so clearly I don't recognize this new tab (even if it appears to have focus).

Secondly, am I correct in assuming that I need to connect to the link/url first, then attach to the browser tab, then I can continue with populating fields?

Lastly, is there a short name url I can use, as the url generated is very long, or do we need to insert the entire url?

Upvotes: 3

Views: 5352

Answers (1)

Sveatoslav
Sveatoslav

Reputation: 823

I would recommend using watir-webdriver on tab handling:

Try the following

MEDBEDbs-iMac:~ medbedb$ irb
1.9.3p392 :001 > require 'watir-webdriver'
 => true 
1.9.3p392 :002 > b = Watir::Browser.new :chrome
 => #<Watir::Browser:0x..f87e94a30e87e1e60 url="about:blank" title="about:blank"> 
1.9.3p392 :003 > b.goto "http://www.w3schools.com/html/html_links.asp"
 => "http://www.w3schools.com/html/html_links.asp" 
1.9.3p392 :004 > b.title
 => "HTML Links" 

Here a new window is opened in a new tab.

1.9.3p392 :005 > b.a(:text, 'HTML links').click
 => [] 
1.9.3p392 :006 > b.windows.count
 => 2 

But watir-webdriving is still handling the previous page..

1.9.3p392 :007 > b.title
 => "HTML Links" 

Switching to recently opened tab.

1.9.3p392 :008 > b.windows.last.use
 => #<Watir::Window:0x..fd949289f2b083062 located=true> 
1.9.3p392 :009 > b.title
 => "Tryit Editor v1.7" 

From here you can do whatever you need in a new tab.

1.9.3p392 :010 > b.windows.last.close
 => #<Watir::Window:0x..fd949289f2b083062 located=true> 

After closing the new tab watir will switch back to initial tab.

1.9.3p392 :011 > b.title
 => "HTML Links" 
1.9.3p392 :012 > b.close
 => true 
Show us the code or url to the web-pate to get more detailed results...

Upvotes: 8

Related Questions