Results 1 to 14 of 14

Thread: how to control a webpage via Qt?

  1. #1
    Join Date
    Aug 2010
    Posts
    8
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default how to control a webpage via Qt?

    I want to control a webpage via a Qt program.

    I want to fill in "text-boxes" and click on buttons.

    I was thinking that WebKit will be the way to go, but I can not find a way to modify a given page...

    Any ideas will be appreciated!

    -CGS

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to control a webpage via Qt?

    Go through QWebPage and QWebElement.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Aug 2010
    Posts
    8
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to control a webpage via Qt?

    Hi Wysota,

    Thanks. I gone that far, but I can not find a way to actually modify an element.

    Below my code:

    Qt Code:
    1. void Window::examineChildElements(const QWebElement &parentElement,
    2. QTreeWidgetItem *parentItem)
    3. {
    4. QWebElement element = parentElement.firstChild();
    5. while (!element.isNull()) {
    6.  
    7. item->setText(0, element.tagName());
    8. parentItem->addChild(item);
    9.  
    10. if (element.tagName()=="TEXTAREA")
    11. --->>>//How do I modify the content of TEXTAREA? <<<----
    12.  
    13.  
    14. examineChildElements(element, item);
    15.  
    16. element = element.nextSibling();
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    My questions are:

    - Should I use the tagName to identify items?
    - And once I find an element (i.e. a texBox, how do I modify it's contents? (i.e. fill-in a TextBox, check a combobox, click on a button?)

    Thanks in advance!

    --CGS

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to control a webpage via Qt?

    You are manipulating DOM here. I'd guess for TEXTAREA you should set the "value" attribute of the element or possibly add a text child to the element. To check a checkbox you probably need to set the "checked" attribute to "true", etc. To click on a button it's probably easiest to evaluate a proper javascript:
    Qt Code:
    1. element.evaluateJavaScript("this.click()");
    To copy to clipboard, switch view to plain text mode 
    You can modify the contents with javascript too:
    Qt Code:
    1. textarea.evaluateJavaScript("this.value = \"some new value for text area\"");
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    tamalotes (30th August 2010)

  6. #5
    Join Date
    Aug 2010
    Posts
    8
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to control a webpage via Qt?

    Master of Zen, Thanks!

    I guess I am getting closer, but still no luck. My code looks like this:


    Qt Code:
    1. void Window::examineChildElements(const QWebElement &parentElement,
    2. QTreeWidgetItem *parentItem)
    3. {
    4. QWebElement element = parentElement.firstChild();
    5. while (!element.isNull()) {
    6.  
    7. item->setText(0, element.tagName());
    8. parentItem->addChild(item);
    9.  
    10. if (element.tagName()== "TEXTAREA")
    11. {
    12. element.setFocus();
    13. element.evaluateJavaScript("this.value = \"some new value for text area\"");
    14. }
    15.  
    16.  
    17. examineChildElements(element, item);
    18.  
    19. element = element.nextSibling();
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

    It does compile, and I am able to enter to the IF condition, but no change.
    As you probably reconginze this is a snippet from the Qt demos: domtraversal. And I am running this while opening www.google.com

    My code is not exactly what you suggested, as you where using:

    Qt Code:
    1. textarea.evaluateJavaScript("this.value = \"some new value for text area\"");
    To copy to clipboard, switch view to plain text mode 

    But I don't see how I can create a "textarea"....

    Any comments will be apprecaited?

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to control a webpage via Qt?

    Did you try the approach with adding a child node to the textarea tag?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Aug 2010
    Posts
    8
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to control a webpage via Qt?

    Hi Master,

    addChild is not available for "element" so I assume using one of the append options was what you suggested.

    This is what I got using appendInside:

    Qt Code:
    1. if (element.tagName()== "TEXTAREA")
    2. {
    3. QWebElement textArea;
    4. element.appendInside(textArea);
    5. textArea.evaluateJavaScript("this.value = \"some new value for text area\"");
    6. }
    To copy to clipboard, switch view to plain text mode 

    But my textbox does not work.... I guess I do not understand how to create a child, but not sure then what to use.

    -CGS

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to control a webpage via Qt?

    You were supposed to add the text you want the textarea to contain as a child element to the textarea element.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #9
    Join Date
    Aug 2010
    Posts
    8
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to control a webpage via Qt?

    Hi Master of Zen,

    If I understand correctly what you are saying, is the I should:

    Qt Code:
    1. void Window::examineChildElements(const QWebElement &parentElement,
    2. QTreeWidgetItem *parentItem)
    3. {
    4. QWebElement element = parentElement.firstChild();
    5. while (!element.isNull()) {
    6.  
    7. item->setText(0, element.tagName());
    8. parentItem->addChild(item);
    9.  
    10. if (element.tagName()== "TEXTAREA")
    11. {
    12. QWebElement textArea;
    13. element.addChild(textArea);
    14.  
    15. //textArea.evaluateJavaScript("this.value = \"some new value for text area\"");
    16. }
    17.  
    18.  
    19. examineChildElements(element, item);
    20.  
    21. element = element.nextSibling();
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

    However, that generates me the following error:

    'class QWebElement' has no member namded 'addChild'

    Sorry for so many questions. I guess the real issue is that I am not familiar with DOM.

    Is there any place you recommend me exploring?

    Thanks in advance.


    -CgS

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to control a webpage via Qt?

    It is worth having a look at QWebElement documentation to see what methods it has.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #11
    Join Date
    Aug 2010
    Posts
    8
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to control a webpage via Qt?

    Hi Master,

    Thanks. My problem relies on the QWebElement documentation. There is no info about creating a child.

    - CgS

  13. #12
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to control a webpage via Qt?

    Really? In the documentation I clearly see lines such as "Appends the given element as the element's last child".

  14. #13
    Join Date
    Aug 2010
    Posts
    8
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to control a webpage via Qt?

    in retrospective I now understand why Zen Master... you certantly need lots of patience to deal guys like me. I really appreciate the support...

    I guess I am getting close, my code now looks like:

    Qt Code:
    1. void Window::examineChildElements(const QWebElement &parentElement,
    2. QTreeWidgetItem *parentItem)
    3. {
    4. QWebElement element = parentElement.firstChild();
    5. while (!element.isNull()) {
    6.  
    7. item->setText(0, element.tagName());
    8. parentItem->addChild(item);
    9.  
    10. if (element.tagName()== "TEXTAREA")
    11. {
    12. QWebElement textArea;
    13.  
    14. element.appendInside(textArea);
    15.  
    16. textArea.evaluateJavaScript("this.value = \"some new value for text area\"");
    17. }
    18.  
    19.  
    20. examineChildElements(element, item);
    21.  
    22. element = element.nextSibling();
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

    I does complie, but no text is added to the search box....

    Help is always welcome...

    -CgS

  15. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: how to control a webpage via Qt?

    Let's get one thing straight - you need to append the text you want displayed in the text area to the 'textarea' tag, so that this:
    html Code:
    1. <textarea></textarea>
    To copy to clipboard, switch view to plain text mode 
    becomes this:
    html Code:
    1. <textarea>
    2. This is the text I want to appear in the text box.
    3. </textarea>
    To copy to clipboard, switch view to plain text mode 

    So obviously if you are appending something, this 'something' has to contain the text you want in the text box. And forget about evaluateJavaScript - if it didn't work, then don't put it there.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Cannot open webpage with QWebView
    By szyema in forum Qt Programming
    Replies: 10
    Last Post: 27th February 2012, 10:04
  2. Replies: 0
    Last Post: 16th December 2009, 09:45
  3. qt gui application on webpage
    By bcheruk in forum Qt Programming
    Replies: 4
    Last Post: 23rd April 2009, 20:03
  4. opening webpage using qt....
    By anupamgee in forum Qt Programming
    Replies: 8
    Last Post: 20th April 2009, 11:13

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.