Embed a web browser in a Python program
How can I embed a web b开发者_如何学运维rowser in a Python program? It needs to run on Linux (GTK, Qt are fine), or cross-platform.
I have looked at embedding pywebgtk and Qt's WebKit widget. But these seem to have little more than a rendering engine. In particular, I'd like support for back/forward and tabbed browsing. Is something like this pre-packaged, or do I have to implement it myself?
wxWebConnect seems to be roughly what I was thinking of, but it has no Python bindings.
http://pypi.python.org/pypi/selenium/2.7.0
You can install the selenium package and run a server (same machine, just a different process) with it which you connect to 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 could use subprocess
to start the server inside your python code.
精彩评论