I just got a new job working remotely and I have to start my day by opening a bunch of pages and logging into them. I would love to automate this process as it can be kind of tedious. I would like to leave my personal browsing window alone and open a new window with all of the pages I need. Here is the gist of what I'm trying to do:
import webbrowser
first = True
chromePath = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
URLS = ("first page", "second page", "third page")
for url in URLS:
if first:
webbrowser.get(chromePath).open(url)
first = False
else:
webbrowser.open(url, new=2)
For some reason this code is just opening new tabs in my current browser, which is basically the opposite of what I want it to be doing. What is going on?
I don't have Chrome installed, but there seem to be multiple problems:
webbrowser.get
expects the name of the browser, not the path.webbrowser.get()
and use it to open the remaining urls.import webbrowser
URLS = ("first page", "second page", "third page")
browser= webbrowser.get('chrome')
first= True
for url in URLS:
if first:
browser.open_new(url)
first = False
else:
browser.open_new_tab(url)
May be too late, but probably will help others.
As per the docs, you should try with new=1
webbrowser.open(url, new=0, autoraise=True)
Display url using the default browser. If new is 0, the url is opened in the same browser window if possible. If new is 1, a new browser window is opened if possible. If new is 2, a new browser page (“tab”) is opened if possible. If autoraise is True, the window is raised if possible (note that under many window managers this will occur regardless of the setting of this variable).
Doc link: Webbrowser docs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With