Python webbrowser.open()打开Chrome浏览器
根据文档,它应该在默认浏览器中打开,但出于某种原因,它在我的机器上打开了IE。我在谷歌上搜索了一下,我发现一个答案说我需要注册浏览器,但我不确定如何使用webbrowser.register(),文档似乎不是很清楚。如何注册Chrome,以便将URL传递到webbrowser.open()在Chrome中而不是IE中打开?您可以使用Chrome的路径调用get()。下面是一个示例-将chrome_路径替换为适合您平台的正确路径Python webbrowser.open()打开Chrome浏览器,python,python-3.x,Python,Python 3.x,根据文档,它应该在默认浏览器中打开,但出于某种原因,它在我的机器上打开了IE。我在谷歌上搜索了一下,我发现一个答案说我需要注册浏览器,但我不确定如何使用webbrowser.register(),文档似乎不是很清楚。如何注册Chrome,以便将URL传递到webbrowser.open()在Chrome中而不是IE中打开?您可以使用Chrome的路径调用get()。下面是一个示例-将chrome_路径替换为适合您平台的正确路径 import webbrowser url = 'http://d
import webbrowser
url = 'http://docs.python.org/'
# MacOS
chrome_path = 'open -a /Applications/Google\ Chrome.app %s'
# Windows
# chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
# Linux
# chrome_path = '/usr/bin/google-chrome %s'
webbrowser.get(chrome_path).open(url)
对于Windows,路径使用UNIX样式的路径,因此将反斜杠改为正斜杠
webbrowser.get("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s").open("http://google.com")
见:
您可以通过更改中给出的参数“using”来使用任何其他浏览器,至少在Windows中,该参数必须足够,并且您不必关心浏览器的路径
import webbrowser
url = 'https://stackoverflow.com'
webbrowser.open(url)
注意:对于以上代码行,它仅在windows defualt浏览器(Microsoft Edge)中打开。类似的操作应该可以:
from selenium import webdriver
#driver = webdriver.Firefox()
driver = webdriver.Chrome()
driver.get("http://www.python.org")
在Selenium中,要获取活动选项卡的URL,请尝试
from selenium import webdriver
driver = webdriver.Firefox()
print driver.current_url # This will print the URL of the Active link
发送更改选项卡的信号
再次使用
我在这里只是为您提供一个伪代码
您可以将其放入一个循环中并创建自己的流
我刚接触Stackoverflow,所以仍然在学习如何写正确的答案。在windows中为我工作
请输入chrome应用程序的路径,不要忘记将th%s放在末尾。我仍在尝试用html代码打开浏览器而不保存文件。。。我会在找到方法时添加代码
import webbrowser
chromedir= "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s"
webbrowser.get(chromedir).open("http://pythonprogramming.altervista.org")
>>>链接到:[我的博客中的一个页面,我在这里解释了这一点]这里有一个比较健壮的方法来获得Chrome的路径 (请注意,仅当您特别需要Chrome,而不是默认浏览器、Chrome或其他东西时,才应执行此操作。) 请检查以下内容:
import webbrowser
chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
webbrowser.get(chrome_path).open('http://docs.python.org/')
如果已在windows中设置默认浏览器,则可以执行以下操作:
open\u google=webbrowser.get('windows-default')。open('https://google.com')
//或
open_google=webbrowser.open('https://google.com')
帮助我在google chrome上打开新标签:
import webbrowser
webbrowser.open_new_tab("http://www.google.com")
这是为我玩的一个游戏而做的,它是相关的,所以我要离开它。很简单。从platform.system获取价值。根据不同操作系统的已知值检查它。如果找到匹配项,则为您设置chrome路径。如果未找到任何链接,则会打开指向链接的默认浏览器。希望对某人有用
import time
import os
import webbrowser
import platform
user_OS = platform.system()
chrome_path_windows = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
chrome_path_linux = '/usr/bin/google-chrome %s'
chrome_path_mac = 'open -a /Applications/Google\ Chrome.app %s'
chrome_path = ''
game_site_link = 'https://www.gamelink'
if user_OS == 'Windows':
chrome_path = chrome_path_windows
elif user_OS == 'Linux':
chrome_path = chrome_path_linux
elif user_OS == 'Darwin':
chrome_path = chrome_path_mac
elif user_OS == 'Java':
chrome_path = chrome_path_mac
else:
webbrowser.open_new_tab(game_site_link)
webbrowser.get(chrome_path).open_new_tab(game_site_link)
实际上,我在这里做了更多的修改,因为我还在开发这个启动器,所以它已经更新了
import time
import webbrowser
import platform
import subprocess
import os
import sys
privateServerLink = 'https://www.roblox.com/games/2414851778/TIER-20-Dungeon-Quest?privateServerLinkCode=GXVlmYh0Z7gwLPBf7H5FWk3ClTVesorY'
userBrowserC = input(str("Browser Type: chrome, opera, iexplore, firefox : "))
userSleepTime = int(input("How long do you want it to run?"))
if userBrowserC == 'opera':
userBrowserD = 'launcher.exe'
else:
userBrowserD = userBrowserC
if userBrowserC == "chrome":
taskToKill = "chrome.exe"
else:
taskToKill = "iexplore.exe"
if userBrowserC == 'chrome' and platform.system() == 'Windows':
browserPath = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
elif userBrowserC == 'chrome' and platform.system() == 'Linux':
browserPath = '/usr/bin/google-chrome %s'
elif userBrowserC == 'chrome' and platform.system() == 'Darwin' or
platform.system() == 'Java':
browserPath = 'open -a /Applications/Google\ Chrome.app %s'
elif userBrowserC == 'opera' and platform.system() == 'Windows':
browserPath = 'C:/Users/'+ os.getlogin() +'/AppData/Local/Programs/Opera/launcher.exe'
elif userBrowserC == 'iexplore' and platform.system() == 'Windows':
browserPath = 'C:/Program Files/internet explorer/iexplore.exe %s'
elif userBrowserC == 'firefox' and platform.system() == 'Windows':
browserPath = 'C:/Program Files/Mozilla Firefox/firefox.exe'
else:
browserPath = ''
while 1 == 1:
subprocess.Popen('SynapseX.exe')
time.sleep(7)
webbrowser.get(browserPath).open_new_tab(privateServerLink)
time.sleep(7)
os.system('taskkill /f /im '+taskToKill)
time.sleep(userSleepTime)
我在下面的@mehrdad的答案中找到了我自己问题的答案。以通用方式从Windows查询浏览器路径@mehrdad提供了一个很好的使用Windows注册表的简短代码,但没有包含足够的上下文使其正常工作
import os
import winreg
import shlex
def try_find_chrome_path():
result = None
if winreg:
for subkey in ['ChromeHTML\\shell\\open\\command', 'Applications\\chrome.exe\\shell\\open\\command']:
try: result = winreg.QueryValue(winreg.HKEY_CLASSES_ROOT, subkey)
except WindowsError: pass
if result is not None:
result_split = shlex.split(result, False, True)
result = result_split[0] if result_split else None
if os.path.isfile(result):
break
result = None
else:
expected = "google-chrome" + (".exe" if os.name == 'nt' else "")
for parent in os.environ.get('PATH', '').split(os.pathsep):
path = os.path.join(parent, expected)
if os.path.isfile(path):
result = path
break
return result
print(try_find_chrome_path())
谢谢你的回答@mehrdad
if sys.platform[:3] == "win":
# First try to use the default Windows browser
register("windows-default", WindowsDefault)
# Detect some common Windows browsers, fallback to IE
iexplore = os.path.join(os.environ.get("PROGRAMFILES", "C:\\Program Files"),
"Mozilla Firefox\\FIREFOX.EXE")
for browser in ("firefox", "firebird", "seamonkey", "mozilla",
"netscape", "opera", iexplore):
if shutil.which(browser):
register(browser, None, BackgroundBrowser(browser))
100%工作…请参阅行号535-545..根据您的要求将iexplore的路径更改为firefox或Chrome。。。
在我的例子中,我在上面的firefox设置代码中提到了更改路径
我注意到的一件事是斜杠,在Windows中,路径中需要有两个斜杠,这样就可以了
import webbrowser
chrome_path = "C://Program Files (x86)//Google//Chrome//Application//Chrome.exe %s"
webbrowser.get(chrome_path).open("https://github.com/")
这至少对我有效您也可以使用:
import webbrowser
chrome_path = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
url = "http://docs.python.org/"
webbrowser.register('chrome', None, webbrowser.BackgroundBrowser(chrome_path))
webbrowser.get('chrome').open_new_tab(url)
当您的URL无效时(请确保URL以https://或http://开头,如果没有,请添加),它通常会打开默认值,即。信不信由你,这是最简单的方法
webbrowser.open("www.stackoverflow.com")#just remove https:// or http:// and simply add www.something.com
以这个url为例
https://something.com
如果你愿意给这个url:or something.com
它将在IE中打开
但如果您将以这种方式键入:
它将用铬合金打开
你可以试试这个,这个会有用的
(注意:如果它有另一个后缀,例如,如果您尝试将www添加到它,您的浏览器将抛出一个输入错误)我如何在linux的kiosk模式下打开chrome?不起作用。它执行chrome,但不退出。如何执行chrome并确保其已打开,然后移动到下一个操作?Linux文件访问权限可能会阻止Google chrome加载,例如用户访问权限与根访问权限。我安装了两个版本的Chrome。一个用于根目录,一个用于用户。@Christian你知道为什么“Path/file.exe%s”可以工作,但不是这个:(r“Path/file.exe”)或str(Path/file.exe)?webbrowser库和Selenium之间有什么区别?这对我的工作帮助很大。它执行Chrome,但不退出。@zacharias知道为什么“Path/browser.exe%s”可以工作,但不知道:(r“Path/browser.exe”)或(str(Path/browser.exe))?不工作:webbrowser.get(“Path”)webbrowser.open(“url”)工作:webbrowser.get(“Path”).open(“url”)(知道为什么吗?)谢谢!它成功了:)您能告诉我如何在python浏览器中获得响应吗?在我的例子中,它只是简单的JSON响应。OVE代码将在Firefox和Chrome浏览器中打开URL,而不是默认浏览器(IE)。这甚至不是使用同一个库?它使用webbrowser模块,所以不要忘记通过以下方式导入:
import webbrowser
记住,你也可以用硒来做这件事。你的方法看起来很有希望,但你的答案不完整。为了使方法正常工作,您正在导入哪些Python库?我收到一条错误消息:“NameError:name'\u winreg'未定义”。@RichLysakowskiPhD:谢谢您的快速回答。你在2分钟内回复了!示例中缺少以下导入:导入操作系统、winreg、shlex。然后,唯一需要做的是将变量“_winreg”更改为“winreg”,因为在Python 3中,名称已更改为删除前导下划线。要打开新选项卡,可以使用webbrowser.get(使用='google-chrome')。打开新选项卡(url)我只需在url前面加上“https://”就可以修复它,而且它工作正常。当我没有打开它的边缘。
if sys.platform[:3] == "win":
# First try to use the default Windows browser
register("windows-default", WindowsDefault)
# Detect some common Windows browsers, fallback to IE
iexplore = os.path.join(os.environ.get("PROGRAMFILES", "C:\\Program Files"),
"Mozilla Firefox\\FIREFOX.EXE")
for browser in ("firefox", "firebird", "seamonkey", "mozilla",
"netscape", "opera", iexplore):
if shutil.which(browser):
register(browser, None, BackgroundBrowser(browser))
import webbrowser
chrome_path = "C://Program Files (x86)//Google//Chrome//Application//Chrome.exe %s"
webbrowser.get(chrome_path).open("https://github.com/")
import webbrowser
chrome_path = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
url = "http://docs.python.org/"
webbrowser.register('chrome', None, webbrowser.BackgroundBrowser(chrome_path))
webbrowser.get('chrome').open_new_tab(url)
webbrowser.open("www.stackoverflow.com")#just remove https:// or http:// and simply add www.something.com