How to open a new window on a browser using Selenium WebDriver for python?
How about you do something like this
driver = webdriver.Firefox() #First FF window
second_driver = webdriver.Firefox() #The new window you wanted to open
Depending on which window you want to interact with, you send commands accordingly
print driver.title #to interact with the first driver
print second_driver.title #to interact with the second driver
For all down voters:
The OP asked for "it is only important that a second instance of the browser is opened.
". This answer does not encompass ALL possible requirements of each and everyone's use cases.
The other answers below may suit your particular need.
You can use execute_script
to open new window.
driver = webdriver.Firefox()
driver.get("https://linkedin.com")
# open new tab
driver.execute_script("window.open('https://twitter.com')")
print driver.current_window_handle
# Switch to new window
driver.switch_to.window(driver.window_handles[-1])
print " Twitter window should go to facebook "
print "New window ", driver.title
driver.get("http://facebook.com")
print "New window ", driver.title
# Switch to old window
driver.switch_to.window(driver.window_handles[0])
print " Linkedin should go to gmail "
print "Old window ", driver.title
driver.get("http://gmail.com")
print "Old window ", driver.title
# Again new window
driver.switch_to.window(driver.window_handles[1])
print " Facebook window should go to Google "
print "New window ", driver.title
driver.get("http://google.com")
print "New window ", driver.title
I recommend to use CTRL + N
command on Firefox to reduce less memory usage than to create new browser instances.
import selenium.webdriver as webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Firefox()
body = browser.find_element_by_tag_name('body')
body.send_keys(Keys.CONTROL + 'n')
The way to switch and control windows has already been mentioned by Dhiraj.