2

I am trying to open multiple tabs using this code but its syntax is not having any effect (i.e. the second tab is not opening) nor this code is showing any error. It just opens google and then stops

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(r'C:\chromedriver_win32\chromedriver')
driver.maximize_window()
driver.get('https://google.com')
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')'
finefoot
  • 9,914
  • 7
  • 59
  • 102

2 Answers2

1

This code should work for you :

Code :

driver.maximize_window()
driver.get("https://google.com")
url = "https://www.gmail.com"
driver.execute_script("window.open('"+url+"','_blank');");
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • Thanks bro it worked for me and eased my work.. But i dont understand why keys or action chains are not working.... Because after opening them i want to switch between those tabs – Ants Infinites May 28 '19 at 18:46
0

You can use the below JS

driver.execute_script("window.open('');")

Then you can switch between windows/tabs using:

windows = driver.window_handles
driver.switch_to.window(windows[0])   
driver.switch_to.window(windows[1])   
Nic Laforge
  • 1,776
  • 1
  • 8
  • 14
  • yes bro i tried but i want my code to take url from csv file and run it on different tabs instead of executing in single tab one after the other – Ants Infinites May 27 '19 at 23:39
  • Question was related to how to open a new tab, I have edited the code with how to switch between those tabs after. – Nic Laforge May 27 '19 at 23:57