How do I open tabs using a Watir in ruby
Asked Answered
P

3

5

I'm trying to create additional tabs in Firefox using Watir webdriver. However, I'm not having much luck

After opening the browser:

require 'watir-webdriver' b =Watir::Browser.new

I want to open up tabs but have been unable to figure out how to access the Open a New Tab button on the browser. How could I create a new tab then?

Pilose answered 22/8, 2014 at 17:44 Comment(3)
possible duplicate of Is it possible to open a new tab in watir-webdriver?Factotum
Selenium doesn't support opening tabs.Fleer
Thanks for clearing that upPilose
P
6

As already mentioned Selenium doesn't explicitly support opening of tabs, however there are 'workarounds' such as...

require 'watir-webdriver'
browser = Watir::Browser.new :ff
browser.goto 'http://the-internet.herokuapp.com'
browser.link(:text, 'A/B Testing').click(:command, :shift)
browser.windows.last.use

This will open the link in a new tab (on a mac) and focus it

Pantry answered 23/8, 2014 at 13:32 Comment(5)
I need to do ctrl+T for chrome on windows. This does not work - browser.some_link.click(:control, 'T') C:/blah/selenium-webdriver-2.33.0/lib/selenium/webdriver/common/keyboard.rb:48:in assert_modifier': "T" is not a modifier key, expected one of [:control, :shift, :a from C:/blah/selenium-webdriver-2.33.0/lib/selenium/webdriver/common/keyboard.rb:25:in press' Please help me to fix this.Headforemost
I don't think you need the 't' to open link in new tabs, just the 'ctrl' and 'click'Pantry
oh, yes. ctrl + t is for new tabHeadforemost
but that only opens a new blank tab, you could just 'create'a new instance of the browser if that's all you needPantry
Let us continue this discussion in chat.Pantry
C
4

You can use javascript :

require 'watir' # Crawler
browser = Watir::Browser.new :chrome   #or firefox in your case
browser.goto 'http://example.com'

browser.execute_script('window.open("http://example1.com")')
Caphaitien answered 11/2, 2020 at 9:52 Comment(0)
M
0

Watir does not care if a new page opens in a new window or in a new tab, so use window switching API to switch to the new tab after open it.

http://watir.com/guides/windows/

Moorman answered 2/10, 2015 at 22:6 Comment(1)
while it navigate into another page from the first page would the cookies still remains @Li Xinyang?Blake

© 2022 - 2024 — McMap. All rights reserved.