1

I would like to open multiple chrome windows. Once they open, however, they close at the end of the for loop. can anyone help me? thank you so much

for i in range(numeroTask):
    i = webdriver.Chrome(PATH)   
    i.get("https://www.youtube.com/")
    
AcK
  • 2,063
  • 2
  • 20
  • 27
Davaidd_
  • 27
  • 6

2 Answers2

1

This is how you can do it. I'm using window.open() to open a new tab and then driver.switch_to.window to switch to it, so you can open a url.

from selenium import webdriver

driver = webdriver.Chrome()

windows_count = 3

for i in range(windows_count):
    # Opens a new tab
    driver.execute_script("window.open()")
    # Switch to the newly opened tab
    driver.switch_to.window(driver.window_handles[i])
    # Navigate to new URL in new window
    driver.get("https://youtube.com")

# Close all tabs:
driver.quit() 

Hopefully this helps, good luck!

Updated, way to do it with multiple chrome windows:

from selenium import webdriver

driver = webdriver.Chrome()

windows_count = 3

for i in range(windows_count):
    # Opens a new tab
    driver.execute_script('window.open("https://youtube.com", "_blank", "resizable=yes, scrollbars=yes, titlebar=yes, width=800, height=900, top=10, left=10");')

# Close all windows:
driver.quit()
Svetlana Levinsohn
  • 1,550
  • 3
  • 10
  • 19
  • thank you so much for your answer! But I want to open a lot of chrome windows not tabs in the same browser windows – Davaidd_ Oct 20 '20 at 11:15
  • Oh, I see. You can do anything with that JS really. I updated my answer with another option. It's working super quick, so probably add `sleep`s if you actually want to see what's going on. – Svetlana Levinsohn Oct 20 '20 at 18:40
0

DO you want to open simultaneously? Then you should try threads, async functions.