使用Python在Firefox(win)选项卡上启动网页
我正试图以这种方式使用python在新选项卡中启动网站url,但这两种方式都不起作用: 方法1:使用Python在Firefox(win)选项卡上启动网页,python,windows,command-line,Python,Windows,Command Line,我正试图以这种方式使用python在新选项卡中启动网站url,但这两种方式都不起作用: 方法1: os.system('C:\Program Files\Mozilla Firefox\Firefox.exe -new-tab http://www.google.com/'); 方法2: os.startfile('C:\Program Files\Mozilla Firefox\Firefox.exe -new-tab http://www.google.com/'); 如果我没有添加参数
os.system('C:\Program Files\Mozilla Firefox\Firefox.exe -new-tab http://www.google.com/');
方法2:
os.startfile('C:\Program Files\Mozilla Firefox\Firefox.exe -new-tab http://www.google.com/');
如果我没有添加参数(-new tab),它会工作,打开默认页面。您需要使用该模块
[编辑]
如果要在非默认浏览器中打开url,请尝试:
webbrowser.get('firefox').open_new_tab('http://www.google.com')
使用
os.startfile()
仅传递url。这将导致URL在用户默认浏览器的新选项卡/窗口中打开,这对您的用户更有利。如果您想启动带有参数的程序,则模块更适合:
import subprocess
subprocess.call([r'C:\Program Files\Mozilla Firefox\Firefox.exe',
'-new-tab', 'http://www.google.com/'])
您可能要尝试:
import os
os.spawnl(os.P_NOWAIT, r'C:\Program Files\Mozilla Firefox\Firefox.exe',
r'FireFox', '-new-tab', 'http://www.google.com/')
在没有internet explorer的情况下打开链接并使用firefox,只需确保firefox是默认的web浏览器
import webbrowser
http = 'http://'
links = input()
b = webbrowser.open_new(http + links)
如果您在windows 7计算机(我的设置)上使用python 2.7,如果您使用:
webbrowser.open('google.com')
webbrowser.open('http://google.com')
它将打开旧版windows资源管理器(是的,我知道正确的…)
但是,如果您使用:
webbrowser.open('google.com')
webbrowser.open('http://google.com')
它将在默认web浏览器(在我的例子中是Firefox)中加载url。您可以在webbrowser中使用Mozilla类:
import os
os.chdir('C:\Program Files\Mozilla Firefox') #address of exe file
os.system('firefox.exe') # name of exe file
import webbrowser
firefox = webbrowser.Mozilla("C:\\Program Files\\Mozilla Firefox\\firefox.exe")
firefox.open('http://www.google.com')
在python中,有多种使用不同包打开URL的方法-
使用硒包装-
from selenium import webdriver
browser = webdriver.Chrome(executable_path = '/Users/abcarp/bin/chromedriver')
browser.get('https://in.linkedin.com/')
sleep(10)
browser.close()
下载firefox驱动程序并放置在user/username/bon位置,然后将名称更改为firefox
使用子流程包-
import subprocess
p = subprocess.Popen([r"/Volumes/Firefox/Firefox.app", "http://www.google.com"])
p.kill()
使用机械化软件包-
import mechanize
br = mechanize.Browser()
br.open("http://machinelearningstories.blogspot.com/")
br.close()
使用web浏览器包-
import webbrowser
webbrowser.get('firefox').open_new_tab('http://www.google.com')
关闭已打开的网页-
import os
os.system("taskkill /im chrome.exe /f") #( windows)
os.system("pkill -f Chrome") # mac
这里提到了一些更详细的相同信息-
最好的办法是将FireFox作为默认浏览器,这样就不必将路径指定为FireFox命令help
FireFox--help
--new-tab <url> Open <url> in a new tab.
——在新选项卡中打开新选项卡。
因此,使用
--new tab
而不是--new tab
同样的问题,我需要在Firefox而不是默认浏览器中打开它。如果用户的默认浏览器不是Firefox,你应该强迫他们使用它吗?@Leandro我更改了打开新选项卡的调用,现在应该打开一个新选项卡啊,我注意到它在Windows上不起作用,获取此错误:“找不到可运行的浏览器”嗯,这里有人询问关于模块本身应该在windowsFWIW上工作的相同问题,我只需要将mozilla\firefox安装目录添加到我的路径中——不需要其他环境变量就可以使webbrowser
与firefox和Python 2.7一起工作。如果没有安装firefox怎么办?如果它安装在不同的目录中呢?这不是正确的做法。即使它在某些情况下起作用,也请对您的答案更具描述性,而不仅仅是代码。