简体   繁体   中英

Launch a webpage on a Firefox (win) tab using Python

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.

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')

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/'])

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.

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/')

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)

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.

import os

os.chdir('C:\Program Files\Mozilla Firefox')    #address of exe file

os.system('firefox.exe')   # name of exe file

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')

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

as firefox command help firefox --help

--new-tab <url>    Open <url> in a new tab.

so use --new-tab instead of -new-tab

最好的办法是让 FireFox 作为你的默认浏览器,然后你就不必指定你的路径

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM