W00t
W00t

Reputation: 213

Opening and Closing a webpage in browser from Python

Is there any way to open a browser window with a specified URL, then close the browser at a later point?

Upvotes: 4

Views: 16820

Answers (2)

Andrew
Andrew

Reputation: 5200

The webbrowser module, the easiest way to open a browser window, does not provide a way to close a browser window that it has opened.

For this level of control, try the Selenium module. It's a bit more involved, but offers more control.

Here's an example they give of opening and closing a page:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox()

browser.get('http://www.yahoo.com')
assert 'Yahoo' in browser.title

elem = browser.find_element_by_name('p')  # Find the search box
elem.send_keys('seleniumhq' + Keys.RETURN)

browser.quit()

Upvotes: 2

RanRag
RanRag

Reputation: 49567

Yes, use python's builtin webbrowser module for this.

>>> import webbrowser
>>> url = 'http://www.python.org/'
>>> webbrowser.open_new(url)

Upvotes: 3

Related Questions