2

Code snippet:

import selenium
from selenium import webdriver

driver = webdriver.Chrome()
urls =['fb.com','instgram.com' , 'youtube.com']
for url in urls:
    driver.get(url)
driver.close()

The problem is that it opens the new link on top of the previous one. I want each site to open on a new tab.

1 Answer 1

5

Open the first URL then for the rest, use target: _blank to open each URL in a new tab:

driver.get(urls[0])
for url in urls[1:]:
    driver.execute_script('window.open("{}", "_blank");'.format(url))

And voila, you should have three tabs with three different URLs.

Note about your URLs: These are invalid. Make sure they each have http or https in front of them. And you've accidentally wrote instgram instead of instagram. :P

Sign up to request clarification or add additional context in comments.

2 Comments

Is there any advantage in terms of memory while using this?
Not to my knowledge, although I'm not so familiar with the memory aspects

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.