Launch a webpage on a Firefox (win) tab using Python
Solution 1
You need to use the webbrowser
module
import webbrowser
webbrowser.open('http://www.google.com')
[edit]
If you want to open a url in a non-default browser try:
webbrowser.get('firefox').open_new_tab('http://www.google.com')
Solution 2
If you want to start a program with parameters the subprocess module is a better fit:
import subprocess
subprocess.call([r'C:\Program Files\Mozilla Firefox\Firefox.exe',
'-new-tab', 'http://www.google.com/'])
Solution 3
Use os.startfile()
passing only the url. This will cause the URL to be opened in a new tab/window in the user's default browser, which is much nicer to your user.
Solution 4
there are multiple way of opening URL in python using different packages-
using selenium package-
from selenium import webdriver
browser = webdriver.Chrome(executable_path = '/Users/abcarp/bin/chromedriver')
browser.get('https://in.linkedin.com/')
sleep(10)
browser.close()
download firefox driver and placed at user/username/bon location and change the name to firefox.
using sub-process package-
import subprocess
p = subprocess.Popen([r"/Volumes/Firefox/Firefox.app", "http://www.google.com"])
p.kill()
using mechanize package-
import mechanize
br = mechanize.Browser()
br.open("http://machinelearningstories.blogspot.com/")
br.close()
using web-browser package-
import webbrowser
webbrowser.get('firefox').open_new_tab('http://www.google.com')
closing opened web page-
import os
os.system("taskkill /im chrome.exe /f") #( windows)
os.system("pkill -f Chrome") # mac
same information in some more detail is mentioned here- http://pythonfordatabuggers.blogspot.com/2020/04/automatically-open-and-do-some-actions.html
Solution 5
You might want to try:
import os
os.spawnl(os.P_NOWAIT, r'C:\Program Files\Mozilla Firefox\Firefox.exe',
r'FireFox', '-new-tab', 'http://www.google.com/')
Related videos on Youtube
Comments
-
Leandro Ardissone over 3 years
I'm trying to launch a website url in a new tab using python in that way, but it didn't worked in these both ways:
Method 1:
os.system('C:\Program Files\Mozilla Firefox\Firefox.exe -new-tab http://www.google.com/');
and Method 2:
os.startfile('C:\Program Files\Mozilla Firefox\Firefox.exe -new-tab http://www.google.com/');
If I don't add the parameters (-new-tab http://www.google.com/) it works, opening the default page.
-
Leandro Ardissone about 15 yearsYes, but what if my default browser is not Firefox?
-
Leandro Ardissone about 15 yearsSame issue, I need to open it in Firefox instead of the default browser.
-
Sheep about 15 yearsIf the user's default browser is not Firefox, should you force them to use it?
-
Leandro Ardissone about 15 yearsNice tip. Thanks. But still don't open the page in a new tab instead of new window. Thanks.
-
Leandro Ardissone about 15 years@John: I'm trying to provide a .xpi extension from an URL, so I need the user to open it with Firefox (no Flock or any other associated app).
-
Nadia Alramli about 15 yearswhat if firefox was not installed? what if it was installed in a different directory? this is not the right way to do. Even if it works in some cases
-
Nadia Alramli about 15 years@Leandro I changed the call to open_new_tab it should open a new tab now
-
michelson about 15 yearsInteresting... I've never played with the webbrowser module before. I'll have to stash that one off in the back of my brain for future use. Thanks Nadia.
-
Leandro Ardissone about 15 years@Nadia, awesome! Really easy solution!
-
Leandro Ardissone about 15 yearsAh, I've noticed that it doesn't work on Windows, get this error: "could not locate runnable browser"
-
Nadia Alramli about 15 yearshmm, here is someone asking about the same issue echochamber.me/viewtopic.php?f=20&t=23436 the module itself is supposed to work on windows
-
Leandro Ardissone about 15 yearsCool, I've added the firefox to the BROWSER env var, but no luck, I'll check with the PATH. Thx.
-
martineau over 13 yearsFWIW, I only needed to add the mozilla\firefox installation directory to my PATH -- no other env vars were needed to make
webbrowser
work with firefox and Python 2.7. -
Sree over 10 yearsIs it possible to launch firefox having no menubar, no status bar and no toolbar through python?
-
Louis Yang over 6 years@Leandro Ardissone Add
%s
to the end of the path to open it by Firefox.webbrowser.get('C:/Program Files (x86)/Mozilla Firefox/firefox.exe %s')
-
alan.elkin about 4 yearsPlease be more descriptive with your answer, not just code.