Pyside. Javascript. Execute Js An Get Result As A Pure Text Or Html
Solution 1:
Deciding by PySide documentation for QWebFrame.evaluateJavaScript
, it...
Evaluates the JavaScript defined by scriptSource using this frame as context and returns the result of the last executed statement.
Se definitely it should be returning something from the JS.
Therefore, the only way to get an idea why you're getting None
out of that method invocation, is to know what is the actual value of the "last executed statement" on the JS side. (As a side note, it's possible that the JS side is actually returning a null
or undefined
value, which would be correctly mapped to Python as None
and printed as "None".)
I propose you first try to execute a very very simple snippet of Javascript that returns a constant hardcoded value known to you, and see if that works out. If the problem persists, I would first suggest solving it on that very very simple snippet, and only once that's working, moving on to your real Javascript code.
The lesson of the story is that debugging should start by reducing the problem incrementally so that at some point the problem disappears, and then working out the exact moment that it appears again. Otherwise you're just fighting something you haven't even found/seen.
Solution 2:
The QWebFrame.evaluateJavaScript will do it, but it doesn't work that well. It doesn't always return the right type, and I've found that it always returns None on function calls. One way of getting the result is to set the returned item to a variable you have access to then call evaluateJavaScript again on that variable.
frame.evaluateJavaScript("myVariable = getResult()")
result = frame.evaluateJavaScript("myVariable")
I just looked at the "DukascopyApplet", and its not a typical data type. Only standard data types like strings, ints, float, bools ... can be transferred or returned to python. You will probably have to go through the "DukascopyApplet" and find the specific data you want. Another thing that may help is to attach a Python object to the JavaScript. You can then call that python object's slot methods inside of your JavaScript.
class MyCLass(object):
@QtCore.Slot(str)
def doSomething(self, info):
# do something with the string info here
frame.addToJavaScriptWindowObject("varName", MyClass)
frame.evaluateJavaScript("varName.doSomething(DukascopyApplet.params.height)")
web.settings().setAttribute(QtWebKit.QWebSettings.WebAttribute.DeveloperExtrasEnabled, True)
inspector = QtWebKit.QWebInspector()
inspector.setPage(web.page())
After looking through the DukascopyApplet it doesn't look like there is any useful stored information there. Finding that data may be difficult.
Post a Comment for "Pyside. Javascript. Execute Js An Get Result As A Pure Text Or Html"