PyQT4:“TypeError:尝试不可配置属性的可配置属性。”

时间:2014-11-24 07:12:53

标签: javascript html python-3.x pyqt4

我在使用pyqt在QWebpage中观察html元素的event-propertys时遇到了一些问题。我想用pyqt加载和执行的网页:

<html>
<head>
<title>Test</title>

<script>
/*
 * object.watch polyfill
 *
 * 2012-04-03
 *
 * By Eli Grey, http://eligrey.com
 * Public Domain.
 * NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
 */

    // object.watch
    if (!Object.prototype.watch) {
        console.log("Watch defined...")
        Object.defineProperty(Object.prototype, "watch", {
            enumerable : false,
            configurable : true,
            writable : true,
            value : function(prop, handler) {
                var oldval = this[prop], newval = oldval, getter = function() {
                    return newval;
                }, setter = function(val) {
                    console.log("Set: " + prop);
                    oldval = newval;
                    return newval = handler.call(this, prop, oldval, val);
                };
                console.log("Before if statement...")
                if (true) { //here in original code is: "delete this[prop]", in every browser it is true but in a QWebpage not... why?
                    console.log("After if statement: " + prop
                        + " is observed...")
                    Object.defineProperty(this, prop, {
                        enumerable : true,
                        configurable : true,
                        get : getter,
                        set : function(val) {
                            console.log("Set: " + prop)
                            oldval = newval;
                            var newval = handler.call(this, prop, oldval, val)
                                    || val;
                            return newval;
                        }
                    });
                } else { 
                    console.log("Error: can't be observed")
                }
            }
        });
    }

    // object.unwatch
    if (!Object.prototype.unwatch) {
        Object.defineProperty(Object.prototype, "unwatch", {
            enumerable : false,
            configurable : true,
            writable : false,
            value : function(prop) {
                var val = this[prop];
                delete this[prop]; // remove accessors
                this[prop] = val;
            }
        });
    }
</script>

</head>

<body>

<div id="target" href="#">LINK WITH HANDLER</div>

<script>
    link = document.getElementById("target");
    //
    //link.onclick = myClick2
    link.watch("onclick", function(prop, oldVal, newVal) {
        console.log("watch > onclick has changed!!");
        return newVal;
    });
    link.onclick = myClick;

    function myClick(e) {
        console.log("Dear sir, here is a click");
    }
</script>

当我将其放入html页面并在浏览器(Chrome,Firefox,Epiphany,...)上执行时,我会得到所需的行为,并且可以阅读控制台日志 消息。

但是当我尝试使用pyqt4在python中加载带有QWebpage的页面时(我也试过pyqt5),那么页面的行为方式也不一样。 经过一些测试,我发现问题出现在if语句中。在每个浏览器中都是如此,相比之下使用QWebpage。当我删除它时,我会从标题中收到错误消息。

有人可以告诉我为什么会这样,在使用相同的JavaScript引擎的QWebpage和普通浏览器呈现的网页中有什么不同? 我希望我能提供足够的信息。谢谢你的帮助!

以下是获取html页面的代码:

import sys
import logging
from PyQt4.QtWebKit import QWebPage
from time import time, sleep
from PyQt4.QtCore import QUrl
from PyQt4.Qt import QApplication


class Browser(QWebPage):

def __init__(self, app, proxy = "", port = 0):
    QWebPage.__init__(self)
    self.loadFinished.connect(self._loadFinished)
    self.app = app

def get(self, requested_url, timeout=20):
    logging.debug("Browser started on {}...".format(requested_url))
    self._loading_complete = False
    self.mainFrame().load(QUrl(requested_url))
    t = 0
    while(not self._loading_complete and t < timeout ): # Waiting for finish processing
        self._wait(0.1) 
        t += 0.1

    if not self._loading_complete:
        logging.debug("Timeout Occurs")

    self._analyzing_finished = True
    return self.mainFrame().toHtml()

def _loadFinished(self, result):
        if result:
            self._loading_complete = True

def _wait(self, waittime=1):
    """Wait for delay time
    """
    deadline = time() + waittime
    while time() < deadline:
        sleep(0)
        self.app.processEvents()

def javaScriptConsoleMessage(self, message, lineNumber, sourceID):
    logging.debug("Console: " + message + " at: " + str(lineNumber))       
if __name__ == '__main__':

logging.basicConfig(level=logging.DEBUG,
                format='%(asctime)s: %(levelname)s - %(message)s',
                datefmt='%d.%m.%Y %H:%M:%S',
                #filename='Crawler.log',
                #filemode='w'
                )

app = QApplication(sys.argv)
browser = Browser(app)
browser.get("http://localhost/") #Console output is important

1 个答案:

答案 0 :(得分:0)

我想我可能已经发现为什么返回值delete不同。 javascript规范允许浏览器以他们喜欢的方式实现delete,因此,您似乎无法依赖其返回值的正确性。

举个例子,这里有Firefox(使用Scatchpad)发生的事情:

// page = about:blank

document.body.onclick;
/*
null
*/
document.body.onclick = function(){alert('boo!')};
/*
function (){alert('boo!')}
*/
delete document.body.onclick;
/*
true
*/
document.body.onclick
/*
function (){alert('boo!')}
*/ 

骗子!显然可以删除onclick属性,但无论如何Firefox都会返回true。这让我得出一个初步的结论,即webkit javascript引擎实际上可能在这里做正确的事情,而且其他浏览器也有问题。

有关delete行为的更多信息,请参阅优秀文章Understanding delete,特别是delete and host objects部分。