PDA

View Full Version : how to control a webpage via Qt?



tamalotes
30th August 2010, 21:55
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

wysota
30th August 2010, 22:05
Go through QWebPage and QWebElement.

tamalotes
30th August 2010, 22:36
Hi Wysota,

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

Below my code:


void Window::examineChildElements(const QWebElement &parentElement,
QTreeWidgetItem *parentItem)
{
QWebElement element = parentElement.firstChild();
while (!element.isNull()) {

QTreeWidgetItem *item = new QTreeWidgetItem();
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();
}
}

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

wysota
30th August 2010, 22:46
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:

element.evaluateJavaScript("this.click()");
You can modify the contents with javascript too:

textarea.evaluateJavaScript("this.value = \"some new value for text area\"");

tamalotes
30th August 2010, 23:16
Master of Zen, Thanks!

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



void Window::examineChildElements(const QWebElement &parentElement,
QTreeWidgetItem *parentItem)
{
QWebElement element = parentElement.firstChild();
while (!element.isNull()) {

QTreeWidgetItem *item = new QTreeWidgetItem();
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();
}
}

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:


textarea.evaluateJavaScript("this.value = \"some new value for text area\"");

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

Any comments will be apprecaited?

wysota
31st August 2010, 00:01
Did you try the approach with adding a child node to the textarea tag?

tamalotes
31st August 2010, 13:19
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:



if (element.tagName()== "TEXTAREA")
{
QWebElement textArea;
element.appendInside(textArea);
textArea.evaluateJavaScript("this.value = \"some new value for text area\"");
}

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

wysota
31st August 2010, 14:35
You were supposed to add the text you want the textarea to contain as a child element to the textarea element.

tamalotes
2nd September 2010, 21:16
Hi Master of Zen,

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


void Window::examineChildElements(const QWebElement &parentElement,
QTreeWidgetItem *parentItem)
{
QWebElement element = parentElement.firstChild();
while (!element.isNull()) {

QTreeWidgetItem *item = new QTreeWidgetItem();
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();
}
}

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

wysota
2nd September 2010, 21:28
It is worth having a look at QWebElement documentation to see what methods it has.

tamalotes
4th September 2010, 22:39
Hi Master,

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

- CgS

squidge
5th September 2010, 00:44
Really? In the documentation I clearly see lines such as "Appends the given element as the element's last child".

tamalotes
7th September 2010, 23:07
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:


void Window::examineChildElements(const QWebElement &parentElement,
QTreeWidgetItem *parentItem)
{
QWebElement element = parentElement.firstChild();
while (!element.isNull()) {

QTreeWidgetItem *item = new QTreeWidgetItem();
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();
}
}

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

Help is always welcome...

-CgS

wysota
7th September 2010, 23:23
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:
<textarea></textarea>
becomes this:
<textarea>
This is the text I want to appear in the text box.
</textarea>

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.