Python - how to load Google Chrome or Chromium browser in the gtk.Window like webkit.WebView()?

谁都会走 提交于 2021-01-21 09:51:45

问题


In Python (Linux), how can i load the Google chrome or Chromium browser inside a gtk.Window()?

Where i am using now as webkit but instead of the webkit i need to use Google Chrome/Chromium because of the Javscript engine and other update issues.

$ apt-get install python-webkit
$ cat >> /var/tmp/browser.py << \EOF
#!/usr/bin/env python
import gtk
import webkit
import gobject
gobject.threads_init()
win = gtk.Window()
win.set_title("Python Browser")
bro = webkit.WebView()
bro.open("http://www.google.com")
win.add(bro)
win.show_all()
gtk.main()

EOF
$ python /var/tmp/browser.py

enter image description here


回答1:


I don't think you can embed Chrome ... you can create your application in Qt and embed QtWebkit ... or you can use selenium with whatever driver you wish including Chrome , but i don't think you can embed that .

Qtwebkit has all the features that you need.

EDIT

I take everything back because I just found something that might work. :D

https://bitbucket.org/chromiumembedded/ "A simple framework for embedding chromium browser windows in other applications."

and this framework also has python bindings: http://code.google.com/p/cefpython/

but i'm not sure if chromium has all the features that you need ...




回答2:


To extend sfantu answer. CEF Python comes with examples of embedding the Chrome browser in a PyGTK application, see the screenshot:

https://code.google.com/p/cefpython/wiki/PyGTK

Examples sources (CEF 1 / CEF 3 / Windows / Linux):

https://code.google.com/p/cefpython/source/browse/cefpython/cef1/windows/binaries/pygtk_.py https://code.google.com/p/cefpython/source/browse/cefpython/cef1/linux/binaries_64bit/pygtk_.py https://code.google.com/p/cefpython/source/browse/cefpython/cef3/windows/binaries/pygtk_.py

Embedding the Chrome browser using CEF Python is possible using any framework you like (comes with examples for PyGTK, wxPython, PyQt, PySide, Panda3D, Kivy framework, PyWin32). You just pass a window handle to CEF and the browser is embedded in that window. On Linux it is required to pass a pointer to GtkWindow.




回答3:


According to PyGTK FAQ, it's possible.

More info in tutorial.




回答4:


use

#!/usr/bin/python
# -*- coding: utf-8 -*-
import platform
import os

name = "Google Chrome/Chromium"


def find_path():
    """
    find the chrome executable path on Windows, Linux/OpenBSD, Mac or Unknown system.
    """
    if platform.system() == "Windows":
        return _find_chrome_win()

    elif platform.system() == "Darwin":  # Mac OS
        return _find_chrome_mac()

    elif platform.system() == "Linux" or platform.system() == "OpenBSD":
        return _find_chrome_linux()

    else:
        try:
            return _find_chrome_linux()
        except:
            try:
                return _find_chrome_mac()
            except:
                try:
                    return _find_chrome_win()
                except:
                    return None


def _find_chrome_mac():
    paths = [  # mac os chrome path list
        "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
        "/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary",
        "/Applications/Chromium.app/Contents/MacOS/Chromium",
        "/usr/bin/google-chrome-stable",
        "/usr/bin/google-chrome",
        "/usr/bin/chromium",
        "/usr/bin/chromium-browser",
    ]

    chrome_path = None

    for path in paths:  # loop through paths
        if os.path.exists(path):
            chrome_path = path

    if chrome_path != None:
        return chrome_path

    else:  # use which command to find chrome
        for browser in ("google-chrome", "chrome", "chromium", "chromium-browser"):
            a = os.popen("which " + browser).read()
            if a == "":
                pass
            else:
                return a[:-1]

        return None


def _find_chrome_linux():
    paths = [
        "/usr/bin/google-chrome-stable",  # linux chrome path list
        "/usr/bin/google-chrome",
        "/usr/bin/chromium",
        "/usr/bin/chromium-browser",
        "/snap/bin/chromium",
    ]

    chrome_path = None

    for path in paths:
        if os.path.exists(path):
            chrome_path = path

    if chrome_path != None:
        return chrome_path

    else:  # use which command to find chrome
        for browser in ("google-chrome", "chrome", "chromium", "chromium-browser"):
            a = os.popen("which " + browser).read()
            if a == "":
                pass
            else:
                return a[:-1]

        return None


def _find_chrome_win():
    import winreg as reg  # import registry

    chrome_path = None
    reg_path = r"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe"

    for install_type in (reg.HKEY_CURRENT_USER, reg.HKEY_LOCAL_MACHINE):
        try:
            reg_key = reg.OpenKey(install_type, reg_path, 0, reg.KEY_READ)
            chrome_path = reg.QueryValue(reg_key, None)
            reg_key.Close()
            if not os.path.isfile(chrome_path):
                continue
        except WindowsError:
            chrome_path = None
        else:
            break

    for browser in ("google-chrome", "chrome", "chromium", "chromium-browser"):
        a = os.popen("where " + browser).read()
        if a == "":
            pass
        else:
            chrome_path = a[:-1]

    return chrome_path


chrome_path = find_path()
if not chrome_path:
    raise Exception("No chrome")


class ChromeView:
    def __init__(self, url="data:text/html,<h1>DicksonUI</h1>", options=[]):
        cmd = chrome_path
        for option in options:
            cmd += " "
            cmd += option
        cmd += " --incognito"
        cmd += " --new-window"
        cmd += ' --app="'
        cmd += url + '"'
        os.popen(cmd)

    def version(self, path):
        try:
            v = os.popen(find_path() + " --version").read()
            v2 = v[v.find(" ") + 1 :]
            return int(v2[: v2.find(".")])
        except:
            return None

    def is_chromium(self, path):
        try:
            if os.popen(path + " --version").read().startswith("Chromium"):
                return True
            else:
                return False
        except:
            return None

ChromeView("https://www.google.com")



来源:https://stackoverflow.com/questions/18808809/python-how-to-load-google-chrome-or-chromium-browser-in-the-gtk-window-like-we

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!