Open and close new tab with Selenium WebDriver in OS X
Asked Answered
I

8

20

I'm using the Firefox Webdriver in Python 2.7 on Windows to simulate opening (Ctrl+t) and closing (Ctrl + w) a new tab.

Here's my code:

from selenium import webdriver  
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox()
browser.get('https://www.google.com')
main_window = browser.current_window_handle
# open new tab
browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
browser.get('https://www.yahoo.com')

# close tab
browser.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 'w')

How to achieve the same on a Mac? Based on this comment one should use browser.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't') to open a new tab but I don't have a Mac to test it and what about the equivalent of Ctrl-w?

Thanks!

Involuntary answered 20/9, 2014 at 18:23 Comment(2)
close tab is Command + 'w'. You can think of command key as control key replacement. I guess you will have to borrow one from your friend to test your code...Disinclined
it doesn't work on chrome?!Albigenses
W
13

There's nothing easier and clearer than just running JavaScript.

Open new tab: driver.execute_script("window.open('');")

Wakerly answered 15/1, 2015 at 23:47 Comment(1)
Hi thanks very much! But your command opens a new window, not a new tab. If you show me how to open a new tab with JS I'll accept your answer.Involuntary
L
15

open a new tab:

browser.get('http://www.google.com')

close a tab:

browser.close()

switch to a tab:

browser.swith_to_window(window_name)
Landsturm answered 30/6, 2017 at 3:41 Comment(3)
+1 for browser.close(); I presumed this was to do trash collection on the browser object and did not realize it was a method for closing tabsAngi
@StevenByrne Actually it will cause memory leak if you do not quit the process manually(for example, in python sys.exit(0)) after loop runs, no matter driver.close() or driver.quit().Landsturm
Sorry my bad. driver.quit() will prevent memory leak. And when you have only 1 tab, driver.close() is equal to driver.quit().Landsturm
M
14

You can choose which window you want to close:

window_name = browser.window_handles[0]

Switch window:

browser.switch_to.window(window_name=window_name)

Then close it:

browser.close()
Maltose answered 23/2, 2019 at 10:37 Comment(0)
W
13

There's nothing easier and clearer than just running JavaScript.

Open new tab: driver.execute_script("window.open('');")

Wakerly answered 15/1, 2015 at 23:47 Comment(1)
Hi thanks very much! But your command opens a new window, not a new tab. If you show me how to open a new tab with JS I'll accept your answer.Involuntary
R
7

Just to combine the answers above for someone still curious. The below is based on Python 2.7 and a driver in Chrome.

Open new tab by: driver.execute_script("window.open('"+URL+"', '__blank__');") where URL is a string such as "http://www.google.com".

Close tab by: driver.close() [Note, this also doubles as driver.quit() when you only have 1 tab open].

Navigate between tabs by: driver.switch_to_window(driver.window_handles[0]) and driver.switch_to_window(driver.window_handles[1]).

Ravelment answered 8/6, 2018 at 20:29 Comment(0)
C
2

Open new tab:

browser.execute_script("window.open('"+your url+"', '_blank')")

Switch to new tab:

browser.switch_to.window(windows[1])
Contravallation answered 13/5, 2017 at 23:47 Comment(2)
NameError: name 'windows' is not definedPlayground
windows = browser.window_handlesQuenchless
S
1

This worked for me. When I was doing driver.close and not switching back to old window, the new tab was not closing [I am still not able to figure out why].

driver.execute_script("window.open('');") # Open an empty tab
driver.switch_to.window(driver.window_handles[1]) # Switch to that tab (given you only have two tabs)
driver.get(new_tab_url) # Open url in the new tab
# Do your stuff here###

####################### 
driver.close() # close the new tab
driver.switch_to.window(driver.window_handles[0]) #This step is also important, go back to your old tab (given you have only two tabs)
Schwann answered 17/3, 2023 at 5:47 Comment(2)
Why do we to switch the tabs after .close() ? I did not understand the working of that partMuskrat
In case you have multiple windows open. say 0, 1, 2, 3 (1 was opened from 0). When you do close, you are in 1. You might switch to 2 instead of 0. I was not sure and did not bother to check, which is why I used this. You can skip this step and see what happens and then decide to change the code accordingly. Please push an edit here as well if you doSchwann
B
0

IMHO all the above answers didn't exactly solve the original problem of closing a tab in a window and not closing the entire window or opening a blank tab.

my solution:

browser.switch_to.window("tab1") #change the tab1 to the id of the tab you want to close
browser.execute_script("window.close('','_parent','');")
Benghazi answered 2/12, 2022 at 14:48 Comment(0)
L
0

This worked for me:

url = "https://www.google.com"
#open a new tab    
driver.switch_to.new_window()
driver.get(url)
#add more code here
#....
#close the tab if you have only this tab open driver.close() will also execute driver.quit() and close the browser
driver.close()
#switch back to the ith tab <tab index starts from 0>
driver.switch_to.window(driver.window_handles[i])
Leander answered 23/7, 2024 at 6:18 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.