Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt WebKit
  4. How to access dynamic HTML/DOM elements after executing javascript with QWebView

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

Scheduled Pinned Locked Moved Qt WebKit
2 Posts 2 Posters 3.5k Views
  • 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 Offline
    L Offline
    lzl1010
    wrote on 23 Sept 2014, 10:03 last edited by
    #1

    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
    0
    • B Offline
      B Offline
      brcontainer
      wrote on 1 Oct 2014, 16:48 last edited by
      #2

      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
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved