Open and close new tab with Selenium WebDriver in OS X
Question:
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!
Answers:
There’s nothing easier and clearer than just running JavaScript.
Open new tab:
driver.execute_script("window.open('');")
Open new tab:
browser.execute_script("window.open('"+your url+"', '_blank')")
Switch to new tab:
browser.switch_to.window(windows[1])
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)
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])
.
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()
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','');")
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)
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!
There’s nothing easier and clearer than just running JavaScript.
Open new tab:
driver.execute_script("window.open('');")
Open new tab:
browser.execute_script("window.open('"+your url+"', '_blank')")
Switch to new tab:
browser.switch_to.window(windows[1])
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)
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])
.
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()
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','');")
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)