Python: generic webbrowser.get().open() for chrome.exe does not work
Asked Answered
G

6

4

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\>"

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

If I run the command

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

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


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?

Glabrescent answered 21/7, 2014 at 19:38 Comment(3)
What happens when you do import subprocess; subprocess.Popen([r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe', 'https://google.com']).wait()?Poor
@MartijnPieters: This actually works. :-)Glabrescent
Yes, and dano explained what goes wrong; I didn't realise shlex would do the wrong thing on Windows.Poor
H
10

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")

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 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.

Haberdashery answered 21/7, 2014 at 20:0 Comment(4)
Sounds like something that needs reporting to the Python bug tracker; I cannot find any pre-existing issues.Poor
@MartijnPieters I was in the process of filing one :)Haberdashery
Thanks, @dano: Issue22025 - webbrowser.get(command_line) does not support Windows-style path separatorsGlabrescent
python 3.6 windows7: did not work for me (URL is open in default browser)Lush
E
4

You don't need to switch to Unix-style paths -- simply quote the executable.

import webbrowser
webbrowser.get('"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" %s').open('http://google.com')
Eyelid answered 14/2, 2018 at 15:9 Comment(0)
T
2

Following the suggestions above and working on Windows, to enable Firefox I have changed (and un-commented) the following line in the config file (note the %s at the end):

c.NotebookApp.browser = 'C:/Program Files (x86)/Mozilla Firefox/firefox.exe %s'

This worked for me. Thanks

Threesome answered 13/9, 2017 at 16:59 Comment(1)
What config file do you mean?Glabrescent
D
0

Worked for me

code snippet:

import webbrowser

chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
webbrowser.get(chrome_path).open('http://google.com')
Douglassdougy answered 30/12, 2019 at 7:56 Comment(0)
H
0

On Windows, you don't need to use UNIX-style path. Just wrap the raw string path to google.exe in escaped quotes, and append %s token after it, within an f-string:

import webbrowser

url = "https://docs.python.org/3/library/webbrowser.html"
chrome = r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
webbrowser.get(f"\"{chrome}\" %s").open_new_tab(url)
Hardened answered 19/9, 2020 at 13:34 Comment(0)
J
-1

You can try this:

import webbrowser

chrome_path = "path_where_chrome_is_located"
webbrowser.register('chrome', None, webbrowser.BackgroundBrowser(chrome_path))

webbrowser.get('chrome').open('url')
Jabon answered 13/11, 2020 at 5:29 Comment(1)
Thank you for your answer, however in it's current state it's not formatted very well. Also, looking at the question and the accepted answer, the actual problem the asker was having was with the chrome path itself, which your answer doesn't currently address.Mia

© 2022 - 2024 — McMap. All rights reserved.