5

I'm using selenium on python and want to open a new tab in chrome. I tried

  ActionChains(driver).key_down(Keys.CONTROL).send_keys('t').key_up(Keys.CONTROL).perform()

but that makes no effect. nothing happened. I've also tried

send_keys(Keys.CONTROL+'t')

but that also makes no effect. How can I open a new tab in selenium?

Saritha G
  • 2,588
  • 1
  • 15
  • 27

3 Answers3

2

It is possible to open a new tab the following way:

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

browser = webdriver.Chrome()
browser.get('https://www.google.com')

q = browser.find_element_by_name('q')
q.send_keys(Keys.CONTROL, 't')

browser.close()

Chrome 44.0.2403.89 (64-bit), Selenium 2.46.1 on Ubuntu 12.04.

Notice that I am using , and not a + in the send_keys() function.

Dušan Maďar
  • 9,269
  • 5
  • 49
  • 64
0
ActionChains(driver).key_down(Keys.CONTROL).send_keys(str('\u0074')).perform()

\u0074 represents the character 't'

To press other characters refer the unicode character table - http://unicode.org/charts/PDF/U0000.pdf

Vishal Jagtap
  • 896
  • 11
  • 16
0

For performing such automation operations you might wanna use something that's built over selenium to handle all such tasks like tab switching , automatic element finding , Special Key press and hold :etc.

Take a look at : "webbot"

webbot works even for webpages with dynamically changing id and classnames and has more methods and features than selenium and mechanize.

Here's a small snippet

from webbot import Browser 
web = Browser()
web.go_to('google.com') 
web.click('Sign in')
web.type('mymail@gmail.com' , into='Email')
web.click('NEXT' , tag='span')
web.type('mypassword' , into='Password' , id='passwordFieldId') # specific selection
web.click('NEXT' , tag='span') # you are logged in ^_^

For your specific usage :

web.press( web.Key.CONTROL + 't' )
Natesh bhat
  • 12,274
  • 10
  • 84
  • 125