的Python:通用webbrowser.get()的open()为的chrome.exe不起作用。 [英] Python: generic webbrowser.get().open() for chrome.exe does not work

查看:1714
本文介绍了的Python:通用webbrowser.get()的open()为的chrome.exe不起作用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python 2.7版(8.1运x64)的,我想在Chrome中打开一个URL。
自Chrome只在本机支持3.3+,我试图通用通话:

I am on Python 2.7 (Win 8.1 x64) and I want to open a URL in Chrome. As Chrome is only natively supported in 3.3+, I was trying a generic call:

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

的路径是正确的和打印确实给我一个处理程序:

The path is correct and print does give me a Handler:

"<webbrowser.GenericBrowser object at 0x0000000002D26518\>"

不过,开() - preferably open_new_tab()) - 功能不起作用。返回FALSE。

However, the open() - preferably open_new_tab()) - function does not work. It returns False.

如果我运行命令

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "https://google.com"

在运行对话框窗口,但它确实做的工作,虽然。

in windows run dialog, it does do work, though.

如果我将Chrome设为标准的浏览器和运行

If I set Chrome as standard browser and run

webbrowser.get().open("http://google.com")

它的工作,但它不是我想要的。

it does work, but it's not what I want.

有没有人一个想法是怎么回事?

Has anyone an idea what's going wrong?

推荐答案

您已经在 webbrowser.get 调用中使用UNIX风格的路径:

You have to use unix-style paths in the webbrowser.get call:

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

这是因为 web浏览器内部做了 shlex.split 的路径,这将只是删除Windows风格上路径分隔符:

This is because webbrowser internally does a shlex.split on the path, which will just erase Windows-style path separators:

>>> cmd = "C:\\Users\\oreild1\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe %s"
>>> shlex.split(cmd)
['C:Usersoreild1AppDataLocalGoogleChromeApplicationchrome.exe', '%s']
>>> cmd = "C:/Users/dan/AppData/Local/Google/Chrome/Application/chrome.exe %
s"
>>> shlex.split(cmd)
['C:/Users/dan/AppData/Local/Google/Chrome/Application/chrome.exe', '%s']

shlex 这里如果给出的实际上会做正确的事情POSIX =假关键字参数,但 web浏览器将不提供,即使在Windows上。这可以说是在 web浏览器中的错误

shlex will actually do the right thing here if given the posix=False keyword argument, but webbrowser won't supply that, even on Windows. This is arguably a bug in webbrowser.

这篇关于的Python:通用webbrowser.get()的open()为的chrome.exe不起作用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆