Reputation: 3099
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.
Upvotes: 28
Views: 54807
Reputation: 679
as firefox command help firefox --help
--new-tab <url> Open <url> in a new tab.
so use --new-tab
instead of -new-tab
Upvotes: 0
Reputation: 1
the best thing to do is to make FireFox as your defalt browser then you would not have to specify your path
Upvotes: -2
Reputation: 649
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
Upvotes: 1
Reputation: 1160
you can use Mozilla class in webbrowser:
import webbrowser
firefox = webbrowser.Mozilla("C:\\Program Files\\Mozilla Firefox\\firefox.exe")
firefox.open('http://www.google.com')
Upvotes: 1
Reputation: 69
import os
os.chdir('C:\Program Files\Mozilla Firefox') #address of exe file
os.system('firefox.exe') # name of exe file
Upvotes: 0
Reputation: 179
If you are using python 2.7 on windows 7 machine (my setup), if you use:
webbrowser.open('google.com')
It will open legacy windows explorer (yeah I know right...).
BUT, if you use:
webbrowser.open('http://google.com')
It will load the url in you default web browser, in my case Firefox.
Upvotes: 0
Reputation: 117
opening a link without internet explorer and use firefox, just make sure firefox is the default web browser.
import webbrowser
http = 'http://'
links = input()
b = webbrowser.open_new(http + links)
Upvotes: 0
Reputation: 114953
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')
Upvotes: 63
Reputation: 59563
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/')
Upvotes: 1
Reputation: 229633
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/'])
Upvotes: 12
Reputation: 4259
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.
Upvotes: 3