Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    How to access dynamic HTML/DOM elements after executing javascript with QWebView

    Qt WebKit
    2
    2
    3213
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • L
      lzl1010 last edited by

      Hi

      I use Qt 5.3.2 on Windows platform. I modify the fancybrowser example in Qt5.3.2\Examples\Qt-5.3\webkitwidgets to open a web. The web is correct ok. I process the signal loadfinished() like this.

      //this line is in a .h file
      QWebView* m_pWebView;
      .....

      //this line is in a .cpp file
      m_pWebView = new QWebView(this);
      m_pWebView->load(url);//url
      connect(m_pWebView, SIGNAL(loadFinished(bool)), SLOT(finishLoading(bool)));

      while the web opened, the finishLoading(bool) will be called.
      I use below code to manipulate the web elements.
      QWebFrame *pWebFrame =m_pWebView->page()->mainFrame();
      QWebElement singleFly = pWebFrame->findFirstElement("#returnF");
      singleFly.evaluateJavaScript("this.checked=true");

      QWebElement fromCity = pWebFrame->findFirstElement("#OriCity");
      fromCity.evaluateJavaScript("this.value='CityA'");
      QWebElement fromCityCode = pWebFrame->findFirstElement("#OriCode");
      fromCityCode.evaluateJavaScript("this.value='C'");
      ....

      There are some div is dynamic load and update with ajax in that opened page. I can't get the web element in this div in the finishLoading(bool) function. I even use a timer to reload current page and to get,but I still can't get the element in the dynamic load div .

      Anyone have an idea to access dynamic HTML/DOM elements after executing javascript(ajax)

      I'd appreciate any help someone can give.

      1 Reply Last reply Reply Quote 0
      • B
        brcontainer last edited by

        Try create an Object like this:
        customjs.h
        @class customJS : public QObject
        {
        Q_OBJECT

        public:
        Q_INVOKABLE void customMethod();//called in evaluate

        signals:
        void customSignal();//perfom always call customMethod
        };
        @

        customjs.cpp
        @
        #include "customjs.h"

        void customJS::customMethod () {
        emit customSignal();
        }
        @

        Add object in WebPage/WebFrame:
        @#include "customjs.h"

        JSext = new customJS();//JSext is declared in Main class (eg.: class MainWindow : QMainWindow ...)

        //Added to slot:
        QObject::connect( JSext, SIGNAL(customSignal()), this, SLOT(mySlot()) );

        applyJsToWebView(); //Apply your JS

        QObject::connect(MY_WEB_PAGE->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(applyJsToWebView())); //Apply if cleared js object

        void MainWindow::applyJsToWebView() {
        //customJScallback is the method name in "window object" (javascript object)
        MY_WEB_PAGE->mainFrame()->addToJavaScriptWindowObject(QString("customJScallback"), JSext);
        }

        void MainWindow::mySlot() {
        qDebug() << "Slot called";
        }
        @

        Usage:

        @fromCityCode.evaluateJavaScript(“this.value=‘C’; window.customJScallback();”);@

        Whenever you call the "window.customJScallback", the "customJS::customSignal" signal will be emitted.

        QT project: https://github.com/brcontainer/qt-helper

        1 Reply Last reply Reply Quote 0
        • First post
          Last post