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

2020-05-25 17:42发布

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.

9条回答
Bombasti
2楼-- · 2020-05-25 18:13
import os

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

os.system('firefox.exe')   # name of exe file
查看更多
我只想做你的唯一
3楼-- · 2020-05-25 18:16

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

查看更多
我欲成王,谁敢阻挡
4楼-- · 2020-05-25 18:18

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/')
查看更多
贼婆χ
5楼-- · 2020-05-25 18:19

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.

查看更多
Rolldiameter
6楼-- · 2020-05-25 18:20

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/'])
查看更多
再贱就再见
7楼-- · 2020-05-25 18:23

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')
查看更多
登录 后发表回答