Results 1 to 3 of 3

Thread: QWebView->QWebPage->QWebFrame->evaluateJavaScript returned data

  1. #1
    Join Date
    Jun 2008
    Location
    Kent, UK
    Posts
    5
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QWebView->QWebPage->QWebFrame->evaluateJavaScript returned data

    Hello,

    I am trying to get a list of images from a page. I got as far as downloading the page and running some JavaScript on it. However the returned data is not what I was expecting. I call this:

    Qt Code:
    1. QVariant listOfImages = webView.page()->mainFrame()->evaluateJavaScript("document.getElementsByTagName(\"img\")");
    2. qDebug() << listOfImages;
    To copy to clipboard, switch view to plain text mode 

    The console window shows:
    Debug: QVariant(QVariantMap, QMap(("length", QVariant(double, 20) ) ) )

    Anybody know how I can extract the list of images?

  2. #2
    Join Date
    Jun 2008
    Location
    Kent, UK
    Posts
    5
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QWebView->QWebPage->QWebFrame->evaluateJavaScript returned data

    Ok,

    So I took another route:

    Qt Code:
    1. QVariant listOfImages = webView.page()->mainFrame()->evaluateJavaScript("document.getElementsByTagName(\"img\").length;");
    2. double numberOfImages = listOfImages.toDouble();
    To copy to clipboard, switch view to plain text mode 
    then:
    Qt Code:
    1. for (double i = 0; i < numberOfImages; ++i)
    2. {
    3. srcOfImages = webView.page()->mainFrame()->evaluateJavaScript(QString("document.getElementsByTagName(\"img\")[%1].src;").arg(i));
    4. imageUrl = srcOfImages.toString();
    5. }
    To copy to clipboard, switch view to plain text mode 
    Job done.

  3. The following user says thank you to phannent for this useful post:

    rbp (16th June 2010)

  4. #3
    Join Date
    May 2008
    Location
    Melbourne, Australia
    Posts
    136
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QWebView->QWebPage->QWebFrame->evaluateJavaScript returned data

    that's a neat trick but it feels dirty! Have you found any cleaner alternatives since then?
    Also what about background images referenced in CSS?

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.