PDA

View Full Version : Auto login using java script



johnsoga
14th December 2009, 17:34
I am trying to use QWebpage to parse the data from http://www.wizards.com/dndinsider/compendium/monster.aspx?id=3528. If the user isn't currently logged into the site it will redirect the user to a login page. If the user is redirected to the login page I want to automatically login. When I use the evaluateJavaScript call to fill in the email and password text boxes and submit the form it seems to fail to continue to the desired page. I have attached the test application I put together. Does anyone have any ideas why it wouldn't be working? The code to fill in the form and submit is the following:


void Widget::on_ui_pushButtonLoad_clicked()
{
QWebFrame *frame = webView->page()->mainFrame();
frame->evaluateJavaScript(
QString( "document.getElementById('email').value = \"%1\";" ).
arg( ui_lineEditName->text() ) );
frame->evaluateJavaScript(
QString( "document.getElementById('password').value = \"%1\";" ).
arg( ui_lineEditPassword->text() ) );
}

void Widget::on_ui_pushButtonSubmit_clicked()
{
QWebFrame *frame = webView->page()->mainFrame();
frame->evaluateJavaScript( "document.forms[0].submit();" );
}


Thanks for the help.

johnsoga
14th December 2009, 22:12
So I thought it might be a cookie issue so I added a cookie jar to my QWebPage but that didn't seem to solve the issue either. If I type directly into the QwebView and click submit it works fine but when I try and use JS to fill in the email and password it doesn't work.

piotr.dobrogost
17th April 2010, 03:28
I would be very interested to see solution to this problem.
In the past I've been using
QWebElement::function() with "click" argument with success. This method did not become part of QWebElement API, however. Calling
evaluateJavaScript instead does not seem to work.

The same question was asked here (http://stackoverflow.com/questions/2655414/qt-force-qwebview-to-click-on-a-web-element-even-one-not-visible-on-the-window) and here (http://stackoverflow.com/questions/1219880/how-to-follow-a-link-in-qwebkit) and here (http://www.qtcentre.org/threads/23573-Problem-with-evaluateJavaScript%28%29)

Bebert218
28th April 2011, 11:16
actually I'm having the same problem here with 4.7.2
from what I can say :
- form filling and submission works by hand
- automatic filling works, but then impossible to click manually on the submit button
- if then I modifiy manually the fields of the form, the manual submission is OK
- filling manually and then submit via JS also works correctly
Thus it seems that something goes wrong when assigning the parameters of the forms. But what ? Indeed it works nicely with Firebug, so why not Webkit ?

wysota
28th April 2011, 12:06
It all depends how the webpage is programmed to handle the form. You need to dive into the javascript that drives the form and understand what happens there. You can submit the form by calling submit() on the form or by clicking the submit button (calling click() on the button's web element). Which to choose depends on what the particular webpage does. If it substitutes the form target url when the submit button is clicked by attaching some script to the button's click event then you need to emulate the button click. If the button is disabled because there is some content validation done by the webpage, you either need to submit the form directly (using submit()) or to do something so that the button gets enabled.