PDA

View Full Version : Using QWebEngine to login to a page.



ayanda83
26th November 2016, 02:59
I have a web page that I would like to access using QWebEngine and QWebView but to access this page you must first provide a username and password (which I have). Is there a way to program the username and password so that its automatically provided when this page is called, this will allow me to be logged into the page without having to type-in the username and password all the time. thanking you in advance.

anda_skoa
26th November 2016, 10:39
QWebEnginePage should emit the authenticationRequired() signal when it encounters the login, so you could provide the login data in a slot connected to that.

Cheers,
_

ayanda83
27th November 2016, 13:36
QWebEnginePage should emit the authenticationRequired() signal when it encounters the login, so you could provide the login data in a slot connected to that.
_

I tried using this approach but was unsuccessful. Here (http://web1.capetown.gov.za/web1/TenderPortal/Account/LogOn?ReturnUrl=%2fweb1%2fTenderPortal%2fTender%2f Details%2f116559) is the link of the login page I am trying to automatically log into. I created connection between the authenticationRequired() signal and the mineTenders(const QUrl &requestUrl, QAuthenticator* authenticator) slot, however I don't thing the signal is getting emitted at any point during execution.

I've got this line of code below linked to a pushbutton slot.
web_view->set_Url("http://web1.capetown.gov.za/web1/TenderPortal/Account/LogOn?ReturnUrl=%2fweb1%2fTenderPortal%2fTender%2f Details%2f116559"); The idea is that when I push the button, the above page is suppose to load already logged in. Now code below is my slot with the authenticator and know that this slot can't possibly work, but unfortunately I am a bit lost as to how to achieve my goal.
void WebView::mineTenders(const QUrl &requestUrl, QAuthenticator* authenticator)
{
authenticator->setUser("myemail@someemailserver.com");
authenticator->setPassword("password");
//qDebug() << "signal emitted" <<endl;
}
How can I send the username and password to the server?

anda_skoa
27th November 2016, 14:02
I tried using this approach but was unsuccessful. Here (http://web1.capetown.gov.za/web1/TenderPortal/Account/LogOn?ReturnUrl=%2fweb1%2fTenderPortal%2fTender%2f Details%2f116559) is the link of the login page I am trying to automatically log into.

In thise case the server doesn't ask for regular authentication but presents you with a form.
You will have to programatically fill the form and submit it.



Now code below is my slot with the authenticator and know that this slot can't possibly work

No, that slot looks ok.



How can I send the username and password to the server?
That's what the authenticator does once the signal emit has returned.
I.e. the engine sees the authentication request, it creates an authentication object, emits a pointer to it in a signal, the slot fills in the data, the code after the signal emit uses the data.

A web page using a standard authentication request would have worked with that.

Cheers,
_

ayanda83
28th November 2016, 14:56
In thise case the server doesn't ask for regular authentication but presents you with a form.
You will have to programatically fill the form and submit it.
_

I suspected as much and did as you suggested and it works, see the code below.
void WebView::mineTenders(bool ok)
{
this->page()->runJavaScript("document.getElementById(\"UserName\").value=\"*****@gmail.com\";"
"document.getElementById(\"Password\").value=\"*******@\";"
"document.forms[0].submit();"
"var els = document.querySelectorAll(\"a[href='/web1/TenderPortal/Tender']\");"
"els[0].click();");

}

Now, the problem is that I get logged into the home page and when I try to programmatically redirect to the required page, I get logged out but when I do it manually in the web view, I don't get logged out.

anda_skoa
29th November 2016, 07:47
Do you click on something after the login when you do it manually? And with manually you mean with your applcation and its QWebEngine based view?

How do you do it programmatically?

Cheers,
_

ayanda83
29th November 2016, 07:55
Do you click on something after the login when you do it manually? And with manually you mean with your applcation and its QWebEngine based view?
_

Yes - but of course I want this done automatically by the program.


How do you do it programmatically?
I do this through javascript using the QWebEnginePage::runJavaScript() function. Please see line number 6 and 7 in the code below.

void WebView::mineTenders(bool ok)
{
if(ok == true)
{
this->page()->runJavaScript("document.getElementById(\"UserName\").value=\"*********@*****.com\";"
"document.getElementById(\"Password\").value=\"*******@\";"
"document.forms[0].submit();"
"var els = document.querySelectorAll(\"a[href='/web1/TenderPortal/Tender']\");"
"els[0].click();");
}

}

anda_skoa
29th November 2016, 13:17
And there is no time required after the submit?
I.e. you can click the link without the server processing the login first?

Cheers,
_