Python webbrowser.open() 打开 Chrome 浏览器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22445217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:58:05  来源:igfitidea点击:

Python webbrowser.open() to open Chrome browser

pythonpython-3.x

提问by user1527216

According to the documentation http://docs.python.org/3.3/library/webbrowser.htmlit's supposed to open in the default browser, but for some reason on my machine it opens IE. I did a google search and I came across an answer that said I need to register browsers, but I'm not sure how to use webbrowser.register() and the documentation doesn't seem to be very clear. How do I register Chrome so that urls I pass to webbrowser.open() open in Chrome instead of IE?

根据文档http://docs.python.org/3.3/library/webbrowser.html它应该在默认浏览器中打开,但由于某种原因在我的机器上它打开了 IE。我进行了谷歌搜索,发现一个答案说我需要注册浏览器,但我不确定如何使用 webbrowser.register() 并且文档似乎不是很清楚。如何注册 Chrome,以便我传递给 webbrowser.open() 的 URL 在 Chrome 而不是 IE 中打开?

采纳答案by Chad Jones

You can call get() with the path to Chrome. Below is an example - replace chrome_path with the correct path for your platform.

您可以使用 Chrome 的路径调用 get()。下面是一个示例 - 将 chrome_path 替换为您平台的正确路径。

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)

回答by Zacarias Bendeck

In the case of Windows, the path uses a UNIX-style path, so make the backslash into forward slashes.

在 Windows 的情况下,路径使用 UNIX 样式的路径,因此将反斜杠变成正斜杠。

webbrowser.get("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s").open("http://google.com")

See: Python: generic webbrowser.get().open() for chrome.exe does not work

请参阅:Python:chrome.exe 的通用 webbrowser.get().open() 不起作用

回答by njn1234

import webbrowser 
new = 2 # open in a new tab, if possible

# open a public URL, in this case, the webbrowser docs
url = "http://docs.python.org/library/webbrowser.html"
webbrowser.get(using='google-chrome').open(url,new=new)

you can use any other browser by changing the parameter 'using' as given in a link

您可以通过更改链接中给出的参数“使用”来使用任何其他浏览器

回答by Evgeny Bubnov

at least in Windows it has to be enough and you do not have to take care about path to the browser.

至少在 Windows 中它已经足够了,你不必关心浏览器的路径。

import webbrowser

url = 'https://stackoverflow.com'

webbrowser.open(url)

Note: With the above lines of code, it only opens in windows defualt browser(Microsoft Edge).

注意:使用上面的代码行,它只能在 windows 默认浏览器(Microsoft Edge)中打开。

回答by Logan M

from selenium import webdriver
#driver = webdriver.Firefox()
driver = webdriver.Chrome()
driver.get("http://www.python.org")

回答by Aman Raparia

In Selenium to get the URL of the active tab try,

在 Selenium 中获取活动选项卡的 URL 尝试,

from selenium import webdriver

driver = webdriver.Firefox()
print driver.current_url # This will print the URL of the Active link

Sending a signal to change the tab

发送信号以更改选项卡

driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)

and again use

并再次使用

print driver.current_url

I am here just providing a pseudo code for you.

我在这里只是为您提供一个伪代码。

You can put this in a loop and create your own flow.

您可以将其置于循环中并创建自己的流程。

I new to Stackoverflow so still learning how to write proper answers.

我是 Stackoverflow 的新手,所以仍在学习如何写出正确的答案。

回答by Giovanni G. PY

Worked for me in windows

在 Windows 中为我工作

Put the path of your chrome application and do not forget to put th %s at the end. I am still trying to open the browser with html code without saving the file... I will add the code when I'll find how.

把你的 chrome 应用程序的路径放在最后,不要忘记把 %s 放在最后。我仍在尝试使用 html 代码打开浏览器而不保存文件...我会在找到方法后添加代码。

import webbrowser
chromedir= "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s"
webbrowser.get(chromedir).open("http://pythonprogramming.altervista.org")

>>> link to: [a page from my blog where I explain this]<<<

>>> 链接到:[我博客中的一个页面,我在其中解释了这一点]<<<

回答by user541686

Here's a somewhat robust way to get the path to Chrome.

这是获取 Chrome 路径的一种比较可靠的方法。

(Note that you should do this onlyif you specifically need Chrome, and not the default browser, or Chromium, or something else.)

(请注意,只有当您特别需要 Chrome时才应该这样做,而不是默认浏览器、Chromium 或其他东西。)

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

回答by Rahul Sapparapu

Please check this:

请检查这个:

import webbrowser
chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
webbrowser.get(chrome_path).open('http://docs.python.org/')

回答by Mujeeb Ishaque

If you have set the default browser in windows then you can do this:

如果您在 Windows 中设置了默认浏览器,则可以执行以下操作:

open_google = webbrowser.get('windows-default').open('https://google.com')

// OR

open_google = webbrowser.open('https://google.com')