Can we open a page in a new tab in the same browser using selenium-webdriver?

driver = Selenium::WebDriver.for :firefox
driver.get “https://www.example.com/

driver.find_element(:link_text, “empid”).click

It is opening the page in the current tab.

Any chance to open this link to another tab of the same browser?

And when done some processing and close that new tab, without exiting
the full browser?

Thanks

I highly recommend watir-webdriver, selenium’s API just feels so clumsy
in comparison, and it’s the same underlying driver…

As far as I know, tabs are not supported in webdriver.
However, here’s an alternative:

driver = Selenium::WebDriver.for :firefox
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 )

#Navigate to the url we just extracted
driver.get address

#Once we’re done with that window:
driver.close
driver.switch_to.window( driver.window_handles.first )

Joel P. wrote in post #1095031:

I highly recommend watir-webdriver, selenium’s API just feels so clumsy
in comparison, and it’s the same underlying driver…

As far as I know, tabs are not supported in webdriver.
However, here’s an alternative:

@Joel - > Thanks for your help :slight_smile:

require ‘selenium-webdriver’
driver = Selenium::WebDriver.for :firefox
driver.get “https://www.google.co.in/
address = driver.find_element(:link_text, “Gmail”).attribute(‘href’)
driver.execute_script( “window.open()” )
p driver.window_handles.first #=>
“{f17eac79-daf9-4a6c-a1ff-1b524fef9faf}”
driver.switch_to.window( driver.window_handles.last )
driver.get address
driver.execute_script “window.close()”

driver.execute_script “window.close()”

p driver.window_handles.first #=>
“{f17eac79-daf9-4a6c-a1ff-1b524fef9faf}”
driver.switch_to.window( driver.window_handles.first )
p driver.title #=> “Google”
driver.close #why does here this browser not getting closed?

Works for me. Are you sure you didn’t have another window open?

Actually, come to think of you can skip a step:

driver.execute_script( “window.open(’#{address}’)” )

That negates the need to use “driver.get address”, although I’m not sure
how webdriver will handle the page load waiting using that method.

You can manage browser tabs!

require ‘selenium-webdriver’

driver = Selenium::WebDriver.for :firefox

driver.get “https://www.google.co.in/

p “title on first tab => #{driver.title}”

address = driver.find_element(:link, “Gmail”).attribute “href”

driver.keyboard.send_keys [:control, “t”] # This will open a new tab

driver.get address

driver.keyboard.send_keys [:alt, “2”]

p “title on second tab => #{driver.title}”

email = driver.find_element(:id, “Email”)

email.send_keys “roman.g.rodriguez at gmail.com

driver.keyboard.send_keys [:control, :f4] # This will close actual tab
(#2)

p “title on first tab again=> #{driver.title}”

driver.close