1

I'm trying to open a set of URL links in a web browser in the order they are listed in, but every time I run the below code, the order is different. I'm new to scripting, but understand that lists keep order, which is what I am using below, so I'm confused as to why it isn't working. I suspect it may be due to load time of certain pages, so what can I do to ensure that individual tabs are opened in the listed order, and circumvent any page load time effects on the order?

import webbrowser

urls = ["google.com", "yahoo.com", "bing.com", "duckduckgo.com"]


firefox_path = "C:\\Program Files\\Mozilla Firefox\\firefox.exe"
webbrowser.register('firefox', None, webbrowser.BackgroundBrowser(firefox_path), 1)

for i in urls:
   webbrowser.get('firefox').open_new_tab(i)
KLL
  • 11
  • 1

1 Answers1

0

Your code is correct, however the for loop is too quick for webbrowser to realize that firefox is already being opened.

Simply add a delay to your for loop and it will fix the problem:

import webbrowser
import time

urls = ["google.com", "yahoo.com", "bing.com", "duckduckgo.com"]


firefox_path = "C:\\Program Files\\Mozilla Firefox\\firefox.exe"
webbrowser.register('firefox', None, webbrowser.BackgroundBrowser(firefox_path), 1)

for i in urls:
    webbrowser.get('firefox').open_new_tab(i)
    time.sleep(1)
  • Thank you! That did it. Is there another solution to this that I can pursue to do away with the 1 second delay? In an ideal world, this would function similar to the way Firefox can be set to open new tabs (essentially new tabs are opened, but not loaded until you view/select them). – KLL May 20 '18 at 03:18