Results 1 to 11 of 11

Thread: Get Html element value with QWebEngine

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2015
    Posts
    22
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Post Get Html element value with QWebEngine

    Im porting my QWebkit based app to QWebEngine but im having trouble getting some value of the Html page. this was easily done this way...

    Qt Code:
    1. QWebFrame *webFrame = ui->webView->page()->mainFrame();
    2. QWebElement variable;
    3. variable= webFrame->findChild("#GridView1 td:nth-child(1)");
    To copy to clipboard, switch view to plain text mode 

    but i´ve just read that QWebElement need a rework and it is no included...so my question is: how can i get elements values from the HTML page in a similar way of old Webkit module??

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Get Html element value with QWebEngine

    You could probably write a JavaScript snippet that returns the values you need and run it via http://doc.qt.io/qt-5/qwebenginepage...unJavaScript-1

    Cheers,
    _

  3. #3
    Join Date
    Sep 2011
    Posts
    86
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Get Html element value with QWebEngine

    I'm not sure but maybe it is a matter of wrong syntax. I was using QWebElement to get Html elements and it worked but I used much simpler expressions. You have to try. I don't remember but I probably had problems with using # sign.
    There is also QTextDocument, you can find element in QString with regular expressions and then put it into QTextDocument and use method toPlainText().

  4. #4
    Join Date
    Sep 2015
    Posts
    22
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Get Html element value with QWebEngine

    Quote Originally Posted by anda_skoa View Post
    You could probably write a JavaScript snippet that returns the values you need and run it via http://doc.qt.io/qt-5/qwebenginepage...unJavaScript-1

    Cheers,
    _
    i believe thats not possible because runjavascript function returns void...

    i believe that get the text can be accomplished by using QWebEnginePage selectedText() this function returns a QString of the current selected text, next step is to virtually select the text using javascript and iterate over all table cells but i'm stuck in the part of set the cell value as selected...any ideas?

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Get Html element value with QWebEngine

    Quote Originally Posted by danalex07 View Post
    i believe thats not possible because runjavascript function returns void...
    There is an overload that takes a result callback.

    Cheers,
    _

  6. #6
    Join Date
    Sep 2015
    Posts
    22
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Get Html element value with QWebEngine

    Quote Originally Posted by anda_skoa View Post
    There is an overload that takes a result callback.

    Cheers,
    _
    Ohhhhh yes but i cant make it work, function pointers in Qt make me cry...can u help me a bit??

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Get Html element value with QWebEngine

    The documentation says "functor or lambda" so it depends a bit if you have C++11 available for lambda.
    Qt Code:
    1. runJavaScript(script, [] (const QVariant &result) {
    2. // your callback code
    3. });
    To copy to clipboard, switch view to plain text mode 

    As a functor
    Qt Code:
    1. struct ScriptCallback
    2. {
    3. void operator()(const QVariant &result) {
    4. // your callback code
    5. }
    6. };
    7.  
    8. runJavaScript(script, ScriptCallback());
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  8. The following user says thank you to anda_skoa for this useful post:

    danalex07 (5th February 2016)

  9. #8
    Join Date
    Sep 2015
    Posts
    22
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Get Html element value with QWebEngine

    thanks to anda_skoa i got this far...

    Qt Code:
    1. webFrame->runJavaScript("function myFunction() {var myvar=document.querySelector('#GridView1 td:nth-child(3)');return myvar;} myFunction();",
    2. [] (const QVariant &result) {
    3. qDebug()<<result;
    4. });
    To copy to clipboard, switch view to plain text mode 

    but i'm stuck know in js because it doesn't return the cell value i'm pointing to with the querySelector...maybe wrong syntaxis?

  10. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Get Html element value with QWebEngine

    Have you tried with a simple expression first?
    E.g. a a fix return without a function, then within a function?

    Cheers,
    _

  11. The following user says thank you to anda_skoa for this useful post:

    danalex07 (5th February 2016)

  12. #10
    Join Date
    Sep 2015
    Posts
    22
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Get Html element value with QWebEngine

    Quote Originally Posted by anda_skoa View Post
    Have you tried with a simple expression first?
    E.g. a a fix return without a function, then within a function?

    Cheers,
    _

    yes simple function like "return 3.4" or "function myFunction() {return 3.4;} myFunction();" works fine, but i can't just find a javascript functions that saves table cell value

    <table id="GridView1">
    <tr>
    <td>104534</td>
    <td>04/04/2014</td>
    <td>DM70F36L23CM</td>
    </tr>
    </table>

  13. #11
    Join Date
    Sep 2015
    Posts
    22
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Get Html element value with QWebEngine

    Ok finally i got it...

    for anyone interested in the code


    Qt Code:
    1. webFrame->runJavaScript("function myFunction() {"
    2. "var Row = document.getElementById('GridView1');var Cells = Row.getElementsByTagName('td');"
    3. "return Cells[0].innerText;} myFunction();",
    4. [] (const QVariant &result) {
    5. qDebug()<<result.toString();
    6. });
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 4
    Last Post: 13th November 2015, 17:24
  2. how to change the html element aftter loading
    By lypkiller in forum Qt Programming
    Replies: 0
    Last Post: 26th July 2013, 09:17
  3. Qt supported html subset "table" element RTL problem
    By kalma in forum Qt Programming
    Replies: 0
    Last Post: 8th March 2012, 05:19
  4. Replies: 0
    Last Post: 18th August 2010, 08:22
  5. Replies: 1
    Last Post: 23rd January 2010, 13:16

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.