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/")
    
4
  • Does this answer it stackoverflow.com/a/39037983/7746992 ? Commented Oct 16, 2020 at 21:53
  • I don't need to perform any actions on newly opened windows. I would just open a specific number of windows and keep them open until the program ends Commented Oct 17, 2020 at 9:41
  • Do you need all of them to open a certain url? Bc opening a url is an action on a newly opened window. Commented Oct 19, 2020 at 8:57
  • yes, all windows must show the same url Commented Oct 19, 2020 at 15:23

2 Answers 2

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()
Sign up to request clarification or add additional context in comments.

2 Comments

thank you so much for your answer! But I want to open a lot of chrome windows not tabs in the same browser windows
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 sleeps if you actually want to see what's going on.
0

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

1 Comment

yes simultaneously and then keep them open until I close the program

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.