How to get QWebFrame for iframe / frame QWebElement?

I have a simple Qt GUI application that uses QtWebkit. I am loading a complex page with a lot of IFRAME nested tags. And I want to go through the full DOM tree (like the Chrome browser in the debug panel), including the contents of the frames.

My code is:

QWebElement doc = ui->webView->page()->mainFrame()->documentElement(); QWebElement iframe = doc.findFirst("iframe[id=someid]"); QWebFrame *webFrame = ....? // how to get the QWebFrame for an iframe/frame QWebElement? 

Note: I can navigate through all frames (including nested frames):

 void MainWindow::renderFramesTree(QWebFrame *frame, int indent) { QString s; s.fill(' ', indent * 4); ui->textLog->appendPlainText(s + " " + frame->frameName()); foreach (QWebFrame *child, frame->childFrames()) renderFramesTree(child, indent + 1); } 

But this is not my question. I need to get the corresponding QWebFrame * iframe-QWebElement.

Thanx!

+4
source share
2 answers

Each QWebFrame has a QList<QWebFrame *> QWebFrame::childFrames () const method. Each frame also has a QString QWebFrame::frameName () const . The combination of both can allow you to find what you need.

 QWebFrame * frameImLookingFor = NULL; foreach(QWebFrame * frame, ui->webView->page()->mainFrame()->childFrames()) { if (frame->frameName() == QLatin1String("appFrame")) { frameImLookingFor = frame; break; } } if (frameImLookingFor) { // do what you need } 
+2
source
  • start selector in current frame
  • if it returns something good, we find that our web frame is in the current frame, let's call this element X:
    • now get all frame elements using the iframe, frame selector from the current page
    • go through all these elements and try to match them with the X-frame so you can find the exact index of the X-frame in the document
    • Finally, you can now focus on the child frame with this INDEX
  • else this means that the selector was not found in this frame
    • loop through all the child frames and repeat the entire process for each child frame
0
source

Source: https://habr.com/ru/post/1443579/


All Articles