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
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
Hi Wysota,
Thanks. I gone that far, but I can not find a way to actually modify an element.
Below my code:
Qt Code:
void Window::examineChildElements(const QWebElement &parentElement, { QWebElement element = parentElement.firstChild(); while (!element.isNull()) { item->setText(0, element.tagName()); parentItem->addChild(item); if (element.tagName()=="TEXTAREA") --->>>//How do I modify the content of TEXTAREA? <<<---- examineChildElements(element, item); element = element.nextSibling(); } }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
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:
You can modify the contents with javascript too:Qt Code:
element.evaluateJavaScript("this.click()");To copy to clipboard, switch view to plain text mode
Qt Code:
textarea.evaluateJavaScript("this.value = \"some new value for text area\"");To copy to clipboard, switch view to plain text mode
tamalotes (30th August 2010)
Master of Zen, Thanks!
I guess I am getting closer, but still no luck. My code looks like this:
Qt Code:
void Window::examineChildElements(const QWebElement &parentElement, { QWebElement element = parentElement.firstChild(); while (!element.isNull()) { item->setText(0, element.tagName()); parentItem->addChild(item); if (element.tagName()== "TEXTAREA") { element.setFocus(); element.evaluateJavaScript("this.value = \"some new value for text area\""); } examineChildElements(element, item); element = element.nextSibling(); } }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:
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?
Did you try the approach with adding a child node to the textarea tag?
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:
if (element.tagName()== "TEXTAREA") { QWebElement textArea; element.appendInside(textArea); textArea.evaluateJavaScript("this.value = \"some new value for text area\""); }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
You were supposed to add the text you want the textarea to contain as a child element to the textarea element.
Hi Master of Zen,
If I understand correctly what you are saying, is the I should:
Qt Code:
void Window::examineChildElements(const QWebElement &parentElement, { QWebElement element = parentElement.firstChild(); while (!element.isNull()) { item->setText(0, element.tagName()); parentItem->addChild(item); if (element.tagName()== "TEXTAREA") { QWebElement textArea; element.addChild(textArea); //textArea.evaluateJavaScript("this.value = \"some new value for text area\""); } examineChildElements(element, item); element = element.nextSibling(); } }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
It is worth having a look at QWebElement documentation to see what methods it has.
Hi Master,
Thanks. My problem relies on the QWebElement documentation. There is no info about creating a child.
- CgS
Really? In the documentation I clearly see lines such as "Appends the given element as the element's last child".
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:
void Window::examineChildElements(const QWebElement &parentElement, { QWebElement element = parentElement.firstChild(); while (!element.isNull()) { item->setText(0, element.tagName()); parentItem->addChild(item); if (element.tagName()== "TEXTAREA") { QWebElement textArea; element.appendInside(textArea); textArea.evaluateJavaScript("this.value = \"some new value for text area\""); } examineChildElements(element, item); element = element.nextSibling(); } }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
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:
becomes this:
html Code:
<textarea> This is the text I want to appear in the text box. </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.
Bookmarks