2

I should be able to open a new tab in selenium for python using the code

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

driver = webdriver.Firefox()
driver.get("http://stackoverflow.com/")

body = driver.find_element_by_tag_name("body")
body.send_keys(Keys.COMMAND + 't')

But no new tab opens, and no error message appears (http://stackoverflow.com/ does load).

Note that I am using Keys.COMMAND + 't' because I am running the code on OS X.

I have no idea what is causing the issue as posts like this one, indicate that my code should work.

Updated to include answers

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

driver = webdriver.Firefox()
driver.get("http://stackoverflow.com/")

current_tab = driver.current_window_handle
driver.execute_script('window.open();')
new_tab = [tab for tab in driver.window_handles if tab != current_tab][0]
driver.switch_to.window(new_tab)
driver.get("http://github.com")
inputElement = driver.find_element_by_id("user[login]")
inputElement.send_keys('1')

current_tab = driver.current_window_handle
driver.execute_script('window.open();')
new_tab = [tab for tab in driver.window_handles if tab != current_tab][0]
driver.switch_to.window(new_tab)
driver.get("http://github.com")
inputElement = driver.find_element_by_id("user[email]")
inputElement.send_keys('2')
AzJ
  • 199
  • 1
  • 11
  • 1
    Try `driver.execute_script('window.open();')` instead – Andersson Jun 05 '17 at 17:13
  • @Andersson That worked.... I do not know why. Do you know how to switch to the new open tab. If I can figure that out I will consider the question answered. – AzJ Jun 05 '17 at 17:18

2 Answers2

3

Try below code to open new tab and switch to it:

driver = webdriver.Firefox()
driver.get("http://stackoverflow.com/")

current_tab = driver.current_window_handle
driver.execute_script('window.open("http://github.com");')
new_tab = [tab for tab in driver.window_handles if tab != current_tab][0]
driver.switch_to.window(new_tab)
inputElement = driver.find_element_by_id("user[login]")
inputElement.send_keys('1')

driver.execute_script('window.open("http://github.com");')
third_tab = [tab for tab in driver.window_handles if tab not in (current_tab, new_tab)][0]
driver.switch_to.window(third_tab)
inputElement = driver.find_element_by_id("user[email]")
inputElement.send_keys('2')

You can use driver.close() to close new tab and driver.switch_to.window(current_tab) to switch back to initial tab

Also note that you can pass page URL you want to open in new tab as argument to window.open() like:

driver.execute_script('window.open("https://google.com");')
Andersson
  • 51,635
  • 17
  • 77
  • 129
  • Very helpful. Just one more question if you look at the update that I posted instead of switching to the newest tab the script instead goes back to the first. I am not sure why it does so. – AzJ Jun 05 '17 at 17:41
  • When you search for email input field you don't need to switch again and open Github. Check update – Andersson Jun 05 '17 at 18:06
  • I think that you misunderstood my problem. I am trying to open the same website twice in two different tabs and add text to two different fields. – AzJ Jun 05 '17 at 18:08
  • 1
    Oh... I see. Try `third_tab = [tab for tab in driver.window_handles if tab not in (current_tab, new_tab)][0]` – Andersson Jun 05 '17 at 18:12
  • Works. Excpet that creating a new tab and then loading the webpage need to be two separate commands otherwise it can't find the text field. – AzJ Jun 05 '17 at 18:23
0

Try below code to open new tab in MAC:-

String clickOnTabLink = Keys.chord(Keys.COMMAND, "t", Keys.ENTER);
link.sendKeys(clickOnTabLink);
Juul
  • 642
  • 4
  • 18