我在使用 pyqt 观察 QWebpage 中 html 元素的事件属性时遇到了一些问题。我想用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(我也尝试过 pyqt5)在 python 中使用 QWebpage 加载页面时,页面的行为方式不同。 经过一些测试,我注意到问题出在 if 语句中。在每个浏览器中都是如此,相比之下使用 QWebpage。当我删除它时,我从标题中收到错误消息。
有人能告诉我为什么会这样吗?用 QWebpage 渲染的网页和使用相同 JavaScript 引擎的普通浏览器有什么不同? 我希望我能提供足够的信息。感谢您的帮助!
这是获取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
最佳答案
我想我可能已经发现为什么返回值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 部分.
关于javascript - PyQT4: "TypeError: Attempting to configurable attribute of unconfigurable property.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27099592/