2

Can someone help me with what is wrong with the below code. My intention is to be able to open a new tab in the browser. The script passes, but doesn't really open a new tab

require 'selenium-webdriver'

@browser = Selenium::WebDriver.for :chrome
@browser.navigate.to "http://www.google.com"
body = @browser.find_element(:tag_name => 'body')
body.send_keys(:control, 't')

p "total number of windows"
p @browser.window_handles.length
p "printing window ids"
@browser.window_handles.each do  |window|
  p  window
end
@browser.quit
machzqcq
  • 1,029
  • 13
  • 15

2 Answers2

5

The closest I've got to opening and managing a new tab using Chrome is:

require 'selenium-webdriver'

driver = Selenium::WebDriver.for :chrome
driver.get 'http://www.google.com'

#Extract the link we want to go to
address = driver.find_element(:link_text, "Gmail").attribute('href')

#Open a new browser window
driver.execute_script( "window.open()" )

#Use the newest window
driver.switch_to.window( driver.window_handles.last )
driver.get 'http://yahoo.com'
daremkd
  • 8,244
  • 6
  • 40
  • 66
  • Thank you. This helps, however I could as well create a new webdriver instance and use that. I really want to do the exact operations of ctrl+t which opens a new tab in chrome browser through code. Any more thoughts ? – machzqcq Sep 28 '14 at 23:14
  • It's a Selenium issue, it doesn't support opening new tabs, see http://stackoverflow.com/questions/14550360/selenium-ide-for-firefox-ctrl-tab/14552367#14552367 – daremkd Sep 28 '14 at 23:22
0

If you're on a Mac, try:

body.send_keys(:command, 't')

instead of

body.send_keys(:control, 't')

Look here too: How to open a new tab using Selenium WebDriver with Java?

Community
  • 1
  • 1
Ben
  • 197
  • 3
  • 11
  • I already looked into this link you shared and that is when I posted this question because the behavior is NOT seen in terms of opening a new tab. I am on windows btw. Any more ideas ? – machzqcq Sep 28 '14 at 23:15