Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a new window on a browser using Selenium WebDriver for python?

I am attempting to open a new tab OR a new window in a browser using selenium for python. It is of little importance if a new tab or new window is opened, it is only important that a second instance of the browser is opened.

I have tried several different methods already and none have succeeded.

  1. Switching to a window that does not exist with hopes that it would then open a new window upon failure to locate said window:

    driver.switch_to_window(None)

  2. Iterating through open windows (although there is currently only one)

    for handle in driver.window_handles:
        driver.switch_to_window(handle)
    
  3. Attempting to simulate a keyboard key press

    from selenium.webdriver.common.keys import Keys
    driver.send_keys(Keys.CONTROL + 'T')
    

The problem with this one in particular was that it does not seem possible to send keys directly to the browser, only to a specific element like this:

driver.find_element_by_id('elementID').send_keys(Keys.CONTROL + 'T')

However, when a command such as this is sent to an element, it appears to do absolutely nothing. I attempted to locate the topmost HTML element on the page and send the keys to that, but was again met with failure:

driver.find_element_by_id('wrapper').send_keys(Keys.CONTROL + 'T')

Another version of this I found online, and was not able to verify its validity or lack thereof because I'm not sure what class/module which needs importing

act = ActionChains(driver)
act.key_down(browserKeys.CONTROL)
act.click("").perform()
act.key_up(browserKeys.CONTROL)

Something very similar with different syntax (I'm not sure if one or both of these is correct syntax)

actions.key_down(Keys.CONTROL)
element.send_keys('t')
actions.key_up(Keys.CONTROL)
like image 578
Zach Posten Avatar asked Jun 26 '13 16:06

Zach Posten


People also ask

How do I open a new window in python Selenium?

Open a New Tabget('https://selenium.dev'); // A new tab is opened and switches to it await driver. switchTo(). newWindow('tab'); // Loads Sauce Labs open source website in the newly opened window await driver. get('https://opensource.saucelabs.com/');

Can we open new window in Selenium Webdriver?

We can open a new window on a browser with Selenium webdriver. There are multiple ways to achieve this. Selenium can execute commands in Javascript with the help of the execute_script() method which is one of the ways of opening a new window.

How do I switch to a new window in Python?

switch_to. window method is used for switching between the windows with the help of window_handles ids.

How do I switch to new window in Selenium?

Use the SwitchTo command to switch to the desired window and also pass the URL of the web page.


2 Answers

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.

like image 155
Amey Avatar answered Sep 21 '22 19:09

Amey


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
like image 30
Dhiraj Avatar answered Sep 23 '22 19:09

Dhiraj