在Python程序中嵌入Web浏览器

Mec*_*ail 7 python browser linux

如何在Python程序中嵌入Web浏览器?它需要在Linux(GTK,Qt很好)或跨平台上运行.

我看过嵌入pywebgtk和Qt的WebKit小部件.但这些似乎只不过是一个渲染引擎.特别是,我想支持后退/前进和标签式浏览.是这样的预包装,还是我必须自己实施?

wxWebConnect似乎与我的想法差不多,但它没有Python绑定.

小智 5

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

您可以安装 selenium 包并运行一个服务器(同一台机器,只是不同的进程),您可以使用 python 代码连接到该服务器:

java -jar selenium-server-standalone-2.7.0.jar
Run Code Online (Sandbox Code Playgroud)

然后:

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()
Run Code Online (Sandbox Code Playgroud)

您可以使用subprocesspython 代码来启动服务器。