Using QWebEngine to login to a page.
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.
Re: Using QWebEngine to login to a page.
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,
_
Re: Using QWebEngine to login to a page.
Quote:
Originally Posted by
anda_skoa
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 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.
Code:
web_view->set_Url("http://web1.capetown.gov.za/web1/TenderPortal/Account/LogOn?ReturnUrl=%2fweb1%2fTenderPortal%2fTender%2fDetails%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.
Code:
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?
Re: Using QWebEngine to login to a page.
Quote:
Originally Posted by
ayanda83
I tried using this approach but was unsuccessful.
Here 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.
Quote:
Originally Posted by
ayanda83
Now code below is my slot with the authenticator and know that this slot can't possibly work
No, that slot looks ok.
Quote:
Originally Posted by
ayanda83
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,
_
Re: Using QWebEngine to login to a page.
Quote:
Originally Posted by
anda_skoa
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.
Code:
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.
Re: Using QWebEngine to login to a page.
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,
_
Re: Using QWebEngine to login to a page.
Quote:
Originally Posted by
anda_skoa
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.
Quote:
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.
Code:
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();");
}
}
Re: Using QWebEngine to login to a page.
And there is no time required after the submit?
I.e. you can click the link without the server processing the login first?
Cheers,
_