Is there any way to open a browser window with a specified URL, then close the browser at a later point?
Asked
Active
Viewed 1.6k times
2 Answers
3
Yes, use python's builtin webbrowser module for this.
>>> import webbrowser
>>> url = 'http://www.python.org/'
>>> webbrowser.open_new(url)

RanRag
- 48,359
- 38
- 114
- 167
-
11Which opens, but doesn't provide for closing. – W00t May 26 '12 at 20:30
-
You can check [this link](http://stackoverflow.com/questions/3369073/controlling-browser-using-python) for other alternatives. – RanRag May 26 '12 at 20:38
-
@W00t: Can you please explain the context of your problem as to what you are really trying to achieve by opening and closing browser tabs. – RanRag May 26 '12 at 20:47
-
I'm attempting to load a list of sites with a 60 second delay in between, but the sites cannot be open alongside each other. – W00t May 26 '12 at 20:49
-
ok suppose you open `5 pages` at a delay of `60 seconds` so while closing the webpages you want to close them `one by one` or all at once. – RanRag May 26 '12 at 20:52
-
One by one, as each window cannot be open while another window from my script is open – W00t May 26 '12 at 20:56
-
So, we can do one thing open the 1st url , do the stuff it has to do and than kill that process using python and after 60 sec repeat the above process with 2nd url. – RanRag May 26 '12 at 21:02
-
clicking q while on the page should do – Nour Wolf Dec 16 '13 at 18:58
2
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()

Andrew
- 3,825
- 4
- 30
- 44