Embed a web browser in a Python program

How can I embed a web browser in a Python program? It should work on Linux (GTK, Qt in order) or cross-platform.

I reviewed the pywebgtk attachment and the Qt WebKit widget . But they seem to have little more than a rendering mechanism. In particular, I would like to support back and forth browsing and tab. Is something like this pre-packaged, or should I implement it myself?

wxWebConnect seems to be roughly what I was thinking about, but it has no Python bindings.

+7
source share
1 answer

http://pypi.python.org/pypi/selenium/2.7.0

You can install the selenium package and start the server (the same computer, just a different process) with which you are connecting with your Python code:

java -jar selenium-server-standalone-2.7.0.jar 

then

 from selenium import webdriver from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.common.keys import Keys import time browser = webdriver.Firefox() # Get local session of firefox browser.get("http://www.yahoo.com") # Load page assert "Yahoo!" in browser.title elem = browser.find_element_by_name("p") # Find the query box elem.send_keys("seleniumhq" + Keys.RETURN) time.sleep(0.2) # Let the page load, will be added to the API try: browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]") except NoSuchElementException: assert 0, "can't find seleniumhq" browser.close() 

You can use subprocess to start the server inside your Python code.

+4
source

All Articles