Results 1 to 4 of 4

Thread: QWebPage/QWebFrame find-methods resulting in empty QWebElementCollection

  1. #1
    Join Date
    Jan 2011
    Posts
    18
    Thanks
    6
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default QWebPage/QWebFrame find-methods resulting in empty QWebElementCollection

    Hi,


    i'm struggling with this problem for hours now...i hope someone can enlighten me!

    I'm fetching a HTML-page with an HTTP-Request and read it with QNetworkReply::readAll(). So far so good.
    Then i want to "parse" (get some information of some elements) the HTML-page using QWebPage/QWebFrame.

    And there is my problem. I tried the following, but all the time an empty QWebElementCollection is returned:
    Qt Code:
    1. QWebView view;
    2. view.load( url );
    3. QWebElementCollection elements = view.page()->mainFrame()->findAllElements("div");
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. QWebPage page;
    2. page.mainFrame()->setHtml( reply.readAll() ); //setContent doesn't work either
    3. QWebElementCollection elements = page.mainFrame()->findAllElements("div");
    To copy to clipboard, switch view to plain text mode 
    Only when i set the HTML-Code directly it works just fine as expected and i get the desired elements:
    Qt Code:
    1. QWebPage page;
    2. page.mainFrame()->setContent( QString("<html>...</html>") );
    3. QWebElementCollection elements = page.mainFrame()->findAllElements("div");
    To copy to clipboard, switch view to plain text mode 

    So why doesn't it work when i set the content as QByteArray? Is there some coding issue?
    Note: when i load the same QByteArray into a QWebView it displays just fine.

    Hope someone can finally help me with this!

    EDIT:
    i found out that loadFinished(bool) signal is emited with "false" for the non working cases (with the QByteArray). Thus i subclasse QWebPage and reimplemented the supportsExtension() and extension() in hope to get some info whats going wrong but the methods don't get even called (tested with debugger).
    Qt Code:
    1. virtual bool supportsExtension( Extension extension ) const {
    2. return extension == ErrorPageExtension;
    3. }
    4.  
    5. virtual bool extension ( Extension extension, const ExtensionOption * option = 0, ExtensionReturn * output = 0 )
    6. {
    7. if (extension != QWebPage::ErrorPageExtension)
    8. return false;
    9.  
    10. ErrorPageExtensionOption *errorOption = (ErrorPageExtensionOption*) option;
    11. qDebug() << "Error loading " << qPrintable(errorOption->url.toString());
    12. if(errorOption->domain == QWebPage::QtNetwork)
    13. qDebug() << "Network error (" << errorOption->error << "): ";
    14. else if(errorOption->domain == QWebPage::Http)
    15. qDebug() << "HTTP error (" << errorOption->error << "): ";
    16. else if(errorOption->domain == QWebPage::WebKit)
    17. qDebug() << "WebKit error (" << errorOption->error << "): ";
    18.  
    19. qDebug() << qPrintable(errorOption->errorString);
    20.  
    21. return true;
    22. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by raven-worx; 27th October 2012 at 03:18.

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QWebPage/QWebFrame find-methods resulting in empty QWebElementCollection

    Perhaps I misunderstand, but the following works here:
    Qt Code:
    1. #include <QApplication>
    2. #include <QtWebKit>
    3. #include <QDebug>
    4.  
    5. class Test : QObject{
    6. Q_OBJECT
    7. public:
    8. Test(){
    9. view = new QWebView;
    10. view->load(QUrl("http://news.google.us"));
    11. connect(view, SIGNAL(loadFinished(bool)),this,SLOT(finishedLoading(bool)));
    12. view->show();
    13. }
    14. public slots:
    15. void finishedLoading(bool ok){
    16. QWebElementCollection elements = view->page()->mainFrame()->findAllElements("div");
    17. QList<QWebElement> list = elements.toList();
    18. qDebug() << ok << list.count();
    19. }
    20. private:
    21. QWebView *view;
    22. };
    23.  
    24. #include "main.moc"
    25.  
    26. int main(int argc, char *argv[])
    27. {
    28. QApplication app(argc, argv);
    29. Test test;
    30. return app.exec();
    31. }
    To copy to clipboard, switch view to plain text mode 

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

    raven-worx (27th October 2012)

  4. #3
    Join Date
    Jan 2011
    Posts
    18
    Thanks
    6
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QWebPage/QWebFrame find-methods resulting in empty QWebElementCollection

    yes this works...thank you.

    But i would prefer the solution where i can specify the request all by myself and just pass the received page data to the QWebPage/QWebFrame. It's really a mistery to me why the QByteArrays are not accepted?!
    In the worst case i'll have to go with the loading of the page via QWebView directly.

  5. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QWebPage/QWebFrame find-methods resulting in empty QWebElementCollection

    Quote Originally Posted by raven-worx View Post
    But i would prefer the solution where i can specify the request all by myself and just pass the received page data to the QWebPage/QWebFrame.
    Ah, gotcha. Using QWebFrame::setContent() works in my simple example:
    Qt Code:
    1. #include <QApplication>
    2. #include <QtWebKit>
    3. #include <QtNetwork>
    4. #include <QDebug>
    5.  
    6. class Test : QObject{
    7. Q_OBJECT
    8. public:
    9. Test(){
    10. QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    11. connect(manager, SIGNAL(finished(QNetworkReply*)),
    12. this, SLOT(replyFinished(QNetworkReply*)));
    13. QNetworkRequest request(QUrl("https://news.google.com"));
    14. manager->get(request);
    15. }
    16. public slots:
    17. void replyFinished(QNetworkReply *reply){
    18. QWebPage page;
    19. page.mainFrame()->setContent(reply->readAll());
    20. QWebElementCollection e = page.mainFrame()->findAllElements("div");
    21. QList<QWebElement> list = e.toList();
    22. qDebug() << list.count();
    23. }
    24. };
    25.  
    26. #include "main.moc"
    27.  
    28. int main(int argc, char *argv[])
    29. {
    30. QApplication app(argc, argv);
    31. Test test;
    32. return app.exec();
    33. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by norobro; 27th October 2012 at 04:57. Reason: revised code

Similar Threads

  1. Replies: 0
    Last Post: 29th January 2011, 05:18
  2. Replies: 0
    Last Post: 30th December 2010, 02:04
  3. Replies: 2
    Last Post: 16th June 2010, 15:42
  4. [SOLVED] Always empty QWebPage
    By Guilo in forum Qt Programming
    Replies: 2
    Last Post: 30th December 2009, 17:32

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.