<legend id='QGqA2'><style id='QGqA2'><dir id='QGqA2'><q id='QGqA2'></q></dir></style></legend>
    • <bdo id='QGqA2'></bdo><ul id='QGqA2'></ul>
  1. <tfoot id='QGqA2'></tfoot>

    <small id='QGqA2'></small><noframes id='QGqA2'>

    1. <i id='QGqA2'><tr id='QGqA2'><dt id='QGqA2'><q id='QGqA2'><span id='QGqA2'><b id='QGqA2'><form id='QGqA2'><ins id='QGqA2'></ins><ul id='QGqA2'></ul><sub id='QGqA2'></sub></form><legend id='QGqA2'></legend><bdo id='QGqA2'><pre id='QGqA2'><center id='QGqA2'></center></pre></bdo></b><th id='QGqA2'></th></span></q></dt></tr></i><div id='QGqA2'><tfoot id='QGqA2'></tfoot><dl id='QGqA2'><fieldset id='QGqA2'></fieldset></dl></div>
    2. 有什么方法可以在浏览器中获取 Pyqt5 中页面的

      时间:2023-08-05

        <tbody id='BBkNz'></tbody>

          <legend id='BBkNz'><style id='BBkNz'><dir id='BBkNz'><q id='BBkNz'></q></dir></style></legend>
            <bdo id='BBkNz'></bdo><ul id='BBkNz'></ul>
            1. <tfoot id='BBkNz'></tfoot>

              <small id='BBkNz'></small><noframes id='BBkNz'>

                <i id='BBkNz'><tr id='BBkNz'><dt id='BBkNz'><q id='BBkNz'><span id='BBkNz'><b id='BBkNz'><form id='BBkNz'><ins id='BBkNz'></ins><ul id='BBkNz'></ul><sub id='BBkNz'></sub></form><legend id='BBkNz'></legend><bdo id='BBkNz'><pre id='BBkNz'><center id='BBkNz'></center></pre></bdo></b><th id='BBkNz'></th></span></q></dt></tr></i><div id='BBkNz'><tfoot id='BBkNz'></tfoot><dl id='BBkNz'><fieldset id='BBkNz'></fieldset></dl></div>
                本文介绍了有什么方法可以在浏览器中获取 Pyqt5 中页面的 XPATH 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在使用 pyqt5.我希望用户单击嵌入在我的 pyqt5 应用程序中的浏览器以获取他/她单击它的元素的 XPATH.

                I am working with pyqt5. I want user to click the browser embeded on my pyqt5 application to get the XPATH of the element which he/she is clicking on it.

                知道它是如何完成的,或者是否可行?

                Any idea how it's done, or is it feasible?

                推荐答案

                为了回答这个问题,我使用了以下答案:

                To answer this question I have used the following answers:

                • 获取被点击的项目:https://stackoverflow.com/a/9012576

                获取给定项目的 XPATH:https://stackoverflow.com/a/58677712

                Get the XPATH given the item: https://stackoverflow.com/a/58677712

                运行js脚本并将信息发送到python:https://stackoverflow.com/a/55294356

                Run js scripts and send the information to python: https://stackoverflow.com/a/55294356

                加入所有这些部分,您将获得以下解决方案:

                Joining all these parts you get the following solution:

                ├── main.py
                └── xpath_from_element.js
                

                ma​​in.py

                import os
                
                from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets, QtWebChannel
                
                from jinja2 import Template
                
                CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
                
                
                class Element(QtCore.QObject):
                    def __init__(self, name, parent=None):
                        super(Element, self).__init__(parent)
                        self._name = name
                
                    @property
                    def name(self):
                        return self._name
                
                    def script(self):
                        return ""
                
                
                class WebEnginePage(QtWebEngineWidgets.QWebEnginePage):
                    def __init__(self, parent=None):
                        super(WebEnginePage, self).__init__(parent)
                        self.loadFinished.connect(self.onLoadFinished)
                        self._objects = []
                        self._scripts = []
                
                    def add_object(self, obj):
                        self._objects.append(obj)
                
                    @QtCore.pyqtSlot(bool)
                    def onLoadFinished(self, ok):
                        print("Finished loading: ", ok)
                        if ok:
                            self.load_qwebchannel()
                            self.add_objects()
                
                    def load_qwebchannel(self):
                        file = QtCore.QFile(":/qtwebchannel/qwebchannel.js")
                        if file.open(QtCore.QIODevice.ReadOnly):
                            content = file.readAll()
                            file.close()
                            self.runJavaScript(content.data().decode())
                        if self.webChannel() is None:
                            channel = QtWebChannel.QWebChannel(self)
                            self.setWebChannel(channel)
                
                    def add_objects(self):
                        if self.webChannel() is not None:
                            objects = {obj.name: obj for obj in self._objects}
                            self.webChannel().registerObjects(objects)
                            _script = """
                            {% for obj in objects %}
                            var {{obj}};
                            {% endfor %}
                            new QWebChannel(qt.webChannelTransport, function (channel) {
                            {% for obj in objects %}
                                {{obj}} = channel.objects.{{obj}};
                            {% endfor %}
                            }); 
                            """
                            self.runJavaScript(Template(_script).render(objects=objects.keys()))
                            for obj in self._objects:
                                if isinstance(obj, Element):
                                    self.runJavaScript(obj.script())
                
                
                class Helper(Element):
                    xpathClicked = QtCore.pyqtSignal(str)
                
                    def script(self):
                        js = ""
                        file = QtCore.QFile(os.path.join(CURRENT_DIR, "xpath_from_element.js"))
                        if file.open(QtCore.QIODevice.ReadOnly):
                            content = file.readAll()
                            file.close()
                            js = content.data().decode()
                
                        js += """
                        document.addEventListener('click', function(e) {
                            e = e || window.event;
                            var target = e.target || e.srcElement;
                            var xpath = Elements.DOMPath.xPath(target, false); 
                            {{name}}.receive_xpath(xpath);
                        }, false);"""
                        return Template(js).render(name=self.name)
                
                    @QtCore.pyqtSlot(str)
                    def receive_xpath(self, xpath):
                        self.xpathClicked.emit(xpath)
                
                
                if __name__ == "__main__":
                    import sys
                
                    app = QtWidgets.QApplication(sys.argv)
                
                    xpath_helper = Helper("xpath_helper")
                    xpath_helper.xpathClicked.connect(lambda xpath: print("clicked", xpath))
                    view = QtWebEngineWidgets.QWebEngineView()
                    page = WebEnginePage()
                    page.add_object(xpath_helper)
                    view.setPage(page)
                    view.load(QtCore.QUrl("https://www.qt.io"))
                    view.show()
                    sys.exit(app.exec_())
                

                xpath_from_element.js

                // Copyright 2018 The Chromium Authors. All rights reserved.
                // Use of this source code is governed by a BSD-style license that can be
                // found in the LICENSE file.
                
                Elements = {};
                Elements.DOMPath = {};
                
                /**
                 * @param {!Node} node
                 * @param {boolean=} optimized
                 * @return {string}
                 */
                Elements.DOMPath.xPath = function (node, optimized) {
                    if (node.nodeType === Node.DOCUMENT_NODE) {
                        return '/';
                    }
                
                    const steps = [];
                    let contextNode = node;
                    while (contextNode) {
                        const step = Elements.DOMPath._xPathValue(contextNode, optimized);
                        if (!step) {
                            break;
                        }  // Error - bail out early.
                        steps.push(step);
                        if (step.optimized) {
                            break;
                        }
                        contextNode = contextNode.parentNode;
                    }
                
                    steps.reverse();
                    return (steps.length && steps[0].optimized ? '' : '/') + steps.join('/');
                };
                
                /**
                 * @param {!Node} node
                 * @param {boolean=} optimized
                 * @return {?Elements.DOMPath.Step}
                 */
                Elements.DOMPath._xPathValue = function (node, optimized) {
                    let ownValue;
                    const ownIndex = Elements.DOMPath._xPathIndex(node);
                    if (ownIndex === -1) {
                        return null;
                    }  // Error.
                
                    switch (node.nodeType) {
                        case Node.ELEMENT_NODE:
                            if (optimized && node.getAttribute('id')) {
                                return new Elements.DOMPath.Step('//*[@id="' + node.getAttribute('id') + '"]', true);
                            }
                            ownValue = node.localName;
                            break;
                        case Node.ATTRIBUTE_NODE:
                            ownValue = '@' + node.nodeName;
                            break;
                        case Node.TEXT_NODE:
                        case Node.CDATA_SECTION_NODE:
                            ownValue = 'text()';
                            break;
                        case Node.PROCESSING_INSTRUCTION_NODE:
                            ownValue = 'processing-instruction()';
                            break;
                        case Node.COMMENT_NODE:
                            ownValue = 'comment()';
                            break;
                        case Node.DOCUMENT_NODE:
                            ownValue = '';
                            break;
                        default:
                            ownValue = '';
                            break;
                    }
                
                    if (ownIndex > 0) {
                        ownValue += '[' + ownIndex + ']';
                    }
                
                    return new Elements.DOMPath.Step(ownValue, node.nodeType === Node.DOCUMENT_NODE);
                };
                
                /**
                 * @param {!Node} node
                 * @return {number}
                 */
                Elements.DOMPath._xPathIndex = function (node) {
                    // Returns -1 in case of error, 0 if no siblings matching the same expression,
                    // <XPath index among the same expression-matching sibling nodes> otherwise.
                    function areNodesSimilar(left, right) {
                        if (left === right) {
                            return true;
                        }
                
                        if (left.nodeType === Node.ELEMENT_NODE && right.nodeType === Node.ELEMENT_NODE) {
                            return left.localName === right.localName;
                        }
                
                        if (left.nodeType === right.nodeType) {
                            return true;
                        }
                
                        // XPath treats CDATA as text nodes.
                        const leftType = left.nodeType === Node.CDATA_SECTION_NODE ? Node.TEXT_NODE : left.nodeType;
                        const rightType = right.nodeType === Node.CDATA_SECTION_NODE ? Node.TEXT_NODE : right.nodeType;
                        return leftType === rightType;
                    }
                
                    const siblings = node.parentNode ? node.parentNode.children : null;
                    if (!siblings) {
                        return 0;
                    }  // Root node - no siblings.
                    let hasSameNamedElements;
                    for (let i = 0; i < siblings.length; ++i) {
                        if (areNodesSimilar(node, siblings[i]) && siblings[i] !== node) {
                            hasSameNamedElements = true;
                            break;
                        }
                    }
                    if (!hasSameNamedElements) {
                        return 0;
                    }
                    let ownIndex = 1;  // XPath indices start with 1.
                    for (let i = 0; i < siblings.length; ++i) {
                        if (areNodesSimilar(node, siblings[i])) {
                            if (siblings[i] === node) {
                                return ownIndex;
                            }
                            ++ownIndex;
                        }
                    }
                    return -1;  // An error occurred: |node| not found in parent's children.
                };
                
                /**
                 * @unrestricted
                 */
                Elements.DOMPath.Step = class {
                    /**
                     * @param {string} value
                     * @param {boolean} optimized
                     */
                    constructor(value, optimized) {
                        this.value = value;
                        this.optimized = optimized || false;
                    }
                
                    /**
                     * @override
                     * @return {string}
                     */
                    toString() {
                        return this.value;
                    }
                };
                

                这篇关于有什么方法可以在浏览器中获取 Pyqt5 中页面的 XPATH 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:在 QWidget 上的 QPixmap 上绘制点(pyqt5) 下一篇:使用 QObject 从 Python 线程发出信号

                相关文章

                最新文章

                    • <bdo id='xrMaS'></bdo><ul id='xrMaS'></ul>
                  1. <legend id='xrMaS'><style id='xrMaS'><dir id='xrMaS'><q id='xrMaS'></q></dir></style></legend>

                    <small id='xrMaS'></small><noframes id='xrMaS'>

                    <i id='xrMaS'><tr id='xrMaS'><dt id='xrMaS'><q id='xrMaS'><span id='xrMaS'><b id='xrMaS'><form id='xrMaS'><ins id='xrMaS'></ins><ul id='xrMaS'></ul><sub id='xrMaS'></sub></form><legend id='xrMaS'></legend><bdo id='xrMaS'><pre id='xrMaS'><center id='xrMaS'></center></pre></bdo></b><th id='xrMaS'></th></span></q></dt></tr></i><div id='xrMaS'><tfoot id='xrMaS'></tfoot><dl id='xrMaS'><fieldset id='xrMaS'></fieldset></dl></div>
                    <tfoot id='xrMaS'></tfoot>