QtWebkit how to access dynamic HTML/DOM elements after executing javascript
-
I am modifying the domtraversal example. I traverse the DOM of a webpage using QWebFrame and QWebElements. When I find a certain button, I
@.evaluateJavaScript("this.click()");@
that element and the dynamic content loads on the page. However, the DOM that I'm traversing is still the old one from the original load, before I clicked. How do I update the QWebPage document without reloading the page? If I do
@webView->reload();@
the information I selected is no longer there and this starts an infinite loop of choosing the information and then reloading. I just want to refresh the DOM. I also tried reading the webView->page()->mainFrame()->documentElement() again, but it's still the same DOM.
I'd appreciate any hints or help someone can give.
-
Please provide more information...
Does button.click() change i.e. adds/removes elements or it changes properties of existing elements?
If it creates new content i.e. new web elements you should try to find these web elements starting from webView->page()->mainFrame()->documentElement() again
because your QWebElement is probably holding the old web elementNB! QWebElement could hold even removed web element for a while because you might paste it later
If javascript executed on click() only changes attributes/values of existing web elements well then perhaps it (new content) is accessible only from javascript - I've seen that quite too often, not sure about WebKit
e.g. IMG src="" that was changed from javasript to IMG src="someimg.jpg"
and then 'someimg.jpg' is "visible" only from javascript
i.e. in this case you have to use evaluateJavaScript again to get actual value of src attribute 'someimg.jpg' -
Thanks for the reply ThatDude.
Does button.click() change i.e. adds/removes elements or it changes properties of existing elements?
It adds elements to a static table element. It doesn't change the properties (that I'm aware of), but appends to it.
you should try to find these web elements starting from webView->page()->mainFrame()->documentElement() again
I tried doing that. During debugging I saw that the javascript is not evaluated immediately upon returning to the main loop. Instead, the slot corresponding to the signal I emitted is called immediately and the javascript is not called until sendpostedevents() is reached in the stack. I ended up finding a workaround by not calling the javascript and loading the html I found in the jQuery arguments, but I'm still curious as to how I COULD have reloaded the DOM. It seems that just doing the same
@QWebFrame *frame = webView->page()->mainFrame();
QWebElement document = frame->documentElement();@after the javascript call might work, but I think I would have to add a slot for some signal that told me that the javascript result had been returned. Otherwise the DOM is reloaded before the javascript is even executed.