1
webbrowser.get("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s").open('file://' + os.path.realpath('get.html'))
print('hello')

I have the code above in one of my python files. When I execute it, it opens 'get.html' just fine. However, the execution freezes at that point, the command prompt wont say 'hello' until I close the browser.

Can anyone explain how to fix this issue?

U_A
  • 143
  • 1
  • 8
  • this is probably expected behavior. Your program runs the first line until it gets an exit from the browser program. You can work around this by creating a new process.using multiprocessing – Mohammad Athar Dec 12 '17 at 15:19
  • I had a similar issue, but unfortunately never found a solid solution. The work around I used was simply to use another web browser/driver. For whatever reason, firefox works on my work pc, and chrome works at home. They bother run win7 and the same versions of python. – SuperStew Dec 12 '17 at 15:31

2 Answers2

0

this works for me:

import os
import webbrowser

url = "file://" + os.path.realpath("get.html")
browser_path = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"
#browser_path = "C:/Program Files/Internet Explorer/iexplore.exe"

webbrowser.register("wb", None, webbrowser.BackgroundBrowser(browser_path))
webbrowser.get("wb").open(url)
Maros
  • 1
0

Looking at the source for webbrowser.get i found that you can append & to make it a BackgroundBrowser:

import webbrowser
webbrowser.get("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s &").open("https://stackoverflow.com")

Now it doesn't hang and you can add other arguments such as incognito.

Tried on Windows, Python 3.8

Love
  • 1,709
  • 2
  • 22
  • 30