Results 1 to 8 of 8

Thread: Using QWebEngine to login to a page.

  1. #1
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.
    Last edited by ayanda83; 26th November 2016 at 02:09.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  3. #3
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using QWebEngine to login to a page.

    Quote Originally Posted by anda_skoa View Post
    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.
    Qt Code:
    1. web_view->set_Url("http://web1.capetown.gov.za/web1/TenderPortal/Account/LogOn?ReturnUrl=%2fweb1%2fTenderPortal%2fTender%2fDetails%2f116559");
    To copy to clipboard, switch view to plain text mode 
    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.
    Qt Code:
    1. void WebView::mineTenders(const QUrl &requestUrl, QAuthenticator* authenticator)
    2. {
    3. authenticator->setUser("myemail@someemailserver.com");
    4. authenticator->setPassword("password");
    5. //qDebug() << "signal emitted" <<endl;
    6. }
    To copy to clipboard, switch view to plain text mode 
    How can I send the username and password to the server?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using QWebEngine to login to a page.

    Quote Originally Posted by ayanda83 View Post
    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 View Post
    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 View Post
    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,
    _

  5. #5
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using QWebEngine to login to a page.

    Quote Originally Posted by anda_skoa View Post
    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.
    Qt Code:
    1. void WebView::mineTenders(bool ok)
    2. {
    3. this->page()->runJavaScript("document.getElementById(\"UserName\").value=\"*****@gmail.com\";"
    4. "document.getElementById(\"Password\").value=\"*******@\";"
    5. "document.forms[0].submit();"
    6. "var els = document.querySelectorAll(\"a[href='/web1/TenderPortal/Tender']\");"
    7. "els[0].click();");
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by ayanda83; 28th November 2016 at 14:50.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  7. #7
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using QWebEngine to login to a page.

    Quote Originally Posted by anda_skoa View Post
    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.
    Qt Code:
    1. void WebView::mineTenders(bool ok)
    2. {
    3. if(ok == true)
    4. {
    5. this->page()->runJavaScript("document.getElementById(\"UserName\").value=\"*********@*****.com\";"
    6. "document.getElementById(\"Password\").value=\"*******@\";"
    7. "document.forms[0].submit();"
    8. "var els = document.querySelectorAll(\"a[href='/web1/TenderPortal/Tender']\");"
    9. "els[0].click();");
    10. }
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ayanda83; 29th November 2016 at 11:45.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

Similar Threads

  1. help me create login page with multi-user (admin-user)
    By phovyanz in forum Qt Programming
    Replies: 2
    Last Post: 22nd February 2016, 08:57
  2. create login page
    By smemamian in forum Newbie
    Replies: 9
    Last Post: 20th April 2013, 18:45
  3. Replies: 2
    Last Post: 16th November 2012, 02:39
  4. Creating Login page
    By yuvaraj.yadav in forum Newbie
    Replies: 2
    Last Post: 13th April 2009, 11:37

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.