?
Solved

How do I open new tab or window from selenium python

Posted on 2013-06-26
2
Medium Priority
?
5,578 Views
Last Modified: 2013-12-13
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)

Open in new window


2. Iterating through open windows (although there is currently only one)
            for handle in driver.window_handles:
                  
driver.switch_to_window(handle)

Open in new window


3. Attempting to simulate a keyboard key press:

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

Open in new window


      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')

Open in new window


      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')

Open in new window

     
       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)

Open in new window


      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)

Open in new window

0
Comment
Question by:courtenayt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 25

Accepted Solution

by:
clockwatcher earned 2000 total points
ID: 39286161
Have you tried simply opening one with javascript?

>>> from selenium import webdriver
>>> b = webdriver.Firefox()
>>> b.execute_script("window.open('');")
>>> 

Open in new window


It seems to work for me.
0
 
LVL 1

Author Closing Comment

by:courtenayt
ID: 39287013
Thanks!  Not sure why I couldn't find that anywhere online.
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This guide will walk you through the essential considerations and tech stack for building scalable websites. Know how to grow your business the smart way!
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
An overview on how to enroll an hourly employee into the employee database and how to give them access into the clock in terminal.
This Micro Tutorial will demonstrate how to add subdomains to your content reports. This can be very importing in having a site with multiple subdomains.

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question