machzqcq
machzqcq

Reputation: 1029

open a browser tab using Ruby selenium

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

Upvotes: 2

Views: 8277

Answers (2)

Ben
Ben

Reputation: 318

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?

Upvotes: 0

daremkd
daremkd

Reputation: 8424

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'

Upvotes: 5

Related Questions