Automatic Login Through DOM Automation

Using the DOM allows you to access a website and automatically log in.
It can also automatically insert values into complex “form” elements.

However, DOM automation involves many technical considerations.
For this example, the following methods and functions were added:

METOD & fUNCTOIN List

METHOD Eval( cEval )
METHOD SetValue( cSelector, cValue )
METHOD Click( cSelector )

FUNCTION AWait_SetValue( oWeb, cSelector, cValue )
FUNCTION AWait_Click( oWeb, cSelector )

The example demonstrates automatically entering an ID on Yahoo.com’s login page and clicking the ‘next’ button.

By applying this technique, you can enter desired values into complex forms.

This program provides a way for developers familiar with synchronous programming to interact with the inherently asynchronous TWebView control by emulating synchronous behavior. This can be particularly useful in scenarios where one needs to ensure a webpage is fully loaded or a specific element is present before taking further action.

by Otto with ChatGPT

sample Code

/*
c)copyright 2023- charleskwonohjun@gmail.com ( Seoul, Korea )
*/
#include "FiveWin.ch"

STATIC oWebView  

FUNCTION Main()
    LOCAL oDlg

    DEFINE DIALOG oDlg TITLE "WebView Test" SIZE 1200, 700

         oDlg:bStart := { || GetOuterHtml( oDlg  ) }

    ACTIVATE DIALOG oDlg CENTER
      
RETURN NIL


STATIC FUNCTION GetOuterHtml( oDlg  )
   LOCAL cStr
   LOCAL nCounter := 0
   LOCAL lOk  := .f.

   oWebView := TWebView():New( , oDlg:hWnd )
   oWebView:Navigate( "https://login.yahoo.com/" )

   SysWait()
   Sleep(3000)
   SysWait()
   
   WHILE .T.
      
      ++nCounter
      cStr := AWait_QuerySelector( oWebView, "document.readyState", 3000 )  

      IF cStr == "complete"
         lOk := .t.
         EXIT
      ENDIF      

      SysWait()

      IF nCounter == 10
         EXIT
      ENDIF   

   ENDDO

   IF !lOk
      MsgStop("Timeout! Try again")
      RETURN NIL
   ENDIF
   

   AWait_SetValue( oWebView, "#login-username", "fivewin")
   AWait_Click( oWebView, "#login-signin" )

RETURN NIL   

TAWaitWeb.prg


/*

TAWaitWeb.prg

This is a class for Fivewin TWebView that retrieves data from the WebView in Sync mode.
c)copyright 2023- charleskwonohjun@gmail.com ( Seoul, Korea )

*/

#include "fivewin.ch"

FUNCTION AWait_QuerySelector( oWeb, cQuery, nSecond )
    LOCAL oFunc
    LOCAL cReturn := ""

    oFunc := TAWaitWeb():New( oWeb )    
    oFunc:Query( cQuery, nSecond )

    cReturn := oFunc:cReturn    

RETURN cReturn 

FUNCTION AWait_QuerySelectAll( oWeb, cQuery, nSecond )
    LOCAL oFunc
    LOCAL cReturn := ""

    oFunc := TAWaitWeb():New( oWeb )    
    oFunc:QuerySelectAll( cQuery, nSecond )

    cReturn := oFunc:cReturn    

RETURN cReturn 


FUNCTION AWait_GetOuterHTML( oWeb, cSelector, nSecond )
    LOCAL oFunc
    LOCAL cReturn := ""

    oFunc := TAWaitWeb():New( oWeb )
    oFunc:getOuterHTML( cSelector, nSecond )

    cReturn := oFunc:cReturn    

RETURN cReturn 

FUNCTION AWait_SetValue( oWeb, cSelector, cValue )
    LOCAL oFunc
    
    oFunc := TAWaitWeb():New( oWeb )
    oFunc:SetValue( cSelector, cValue )    

RETURN NIL

FUNCTION AWait_Click( oWeb, cSelector )
    LOCAL oFunc
    
    oFunc := TAWaitWeb():New( oWeb )
    oFunc:Click( cSelector )

RETURN NIL


CLASS TAWaitWeb

    DATA bOldBind
    DATA oWeb
    DATA cQuery
    DATA nSecond

    DATA cReturn 

    METHOD New( oWeb, cQuery, nSecond ) CONSTRUCTOR 
    METHOD GetBind( cJson, cCalls, ... )
    METHOD Query( cQuery, nSecond )
    METHOD QuerySelectAll( cQuery, nSecond )
    
    METHOD GetOuterHtml( cSelecter, nSecond )
    METHOD Eval( cEval )
    METHOD SetValue( cSelector, cValue )
    METHOD Click( cSelector )
    
ENDCLASS    

METHOD New( oWeb ) CLASS TAWaitWeb 

    ::oWeb := oWeb
    ::oWeb:Bind( "SendToFWH" )   
      
RETURN Self

METHOD Query( cQuery, nSecond, cReturn ) CLASS TAWaitWeb 
    LOCAL nCounter := 0
    LOCAL lReturn  := .f.
    LOCAL cEval    := "SendToFWH($QUERY$, 'WebFunc')"
    LOCAL nStartSec := GetTickCount()

    DEFAULT nSecond := 500
    
    ::bOldBind := ::oWeb:bOnBind
    ::oWeb:bOnBind := { | cJson, cCalls, ... | ::GetBind( cJson, cCalls, ... ) }

    cEval := STRTRAN( cEval, "$QUERY$", cQuery ) 
   
    ::cReturn := ""

    WHILE .T.
        ::oWeb:Eval( cEval )

        sleep(100)
        SysWait()
       
        IF !EMPTY( ::cReturn )
            lReturn := .t.
            EXIT
        ENDIF    

        IF GetTickCount() - nStartSec  >= nSecond
           EXIT
        ENDIF
    ENDDO   

    cReturn := ::cReturn

    ::oWeb:bOnBind := ::bOldBind

RETURN lReturn

METHOD QuerySelectAll( cQuery, nSecond, cReturn ) CLASS TAWaitWeb 
    LOCAL nCounter := 0
    LOCAL lReturn  := .f.
    LOCAL cEval    := "SendToFWH(querySelectorAll($QUERY$), 'WebFunc')"
    LOCAL nStartSec := GetTickCount()

    DEFAULT nSecond := 500
    
    ::bOldBind := ::oWeb:bOnBind
    ::oWeb:bOnBind := { | cJson, cCalls, ... | ::GetBind( cJson, cCalls, ... ) }

    cEval := STRTRAN( cEval, "$QUERY$", cQuery ) 
   
    ::cReturn := ""

    WHILE .T.
        ::oWeb:Eval( cEval )

        sleep(100)
        SysWait()
       
        IF !EMPTY( ::cReturn )
           lReturn := .t.
           EXIT
        ENDIF    

        IF GetTickCount() - nStartSec  >= nSecond
           EXIT
        ENDIF    
    ENDDO   

    cReturn := ::cReturn

    ::oWeb:bOnBind := ::bOldBind
    
RETURN lReturn


METHOD GetOuterHtml( cSelecter, nSecond ) CLASS TAWaitWeb 
    LOCAL cQuery := "SendToFWH( document.querySelector('$SELECTER$').outerHTML, 'savehtml')"
    LOCAL cEval
    LOCAL nStartSec := GetTickCount()
    LOCAL lReturn  := .f.

    DEFAULT nSecond := 500

    ::bOldBind := ::oWeb:bOnBind
    ::oWeb:bOnBind := { | cJson, cCalls, ... | ::GetBind( cJson, cCalls, ... ) }

    cEval := STRTRAN( cQuery , "$SELECTER$", cSelecter )
    
    ::cReturn := ""

    WHILE .T.
        ::oWeb:Eval( cEval )

        sleep(100)
        SysWait()        

        IF !EMPTY( ::cReturn )
           lReturn := .t.
           EXIT
        ENDIF    

        IF GetTickCount() - nStartSec  >= nSecond
           EXIT
        ENDIF  
    ENDDO      
        
    ::oWeb:bOnBind := ::bOldBind
    
RETURN lReturn


METHOD GetBind( cJson, cCalls, ... ) CLASS TAWaitWeb 
    LOCAL hResponse := hash()
    
    hb_jsondecode(cJson ,@hResponse )
    
    ::cReturn := ""

    IF LEN( hResponse ) >= 1
       ::cReturn := hResponse[1]     
    ENDIF   

RETURN NIL


METHOD Eval( cEval ) CLASS TAWaitWeb 

    ::oWeb:Eval( cEval )

RETURN NIL

METHOD SetValue( cSelector, cValue ) CLASS TAWaitWeb 
    LOCAL cEval := ""

    cEval := "document.querySelector('$SELECTOR$').value = '$VALUE$'"
    cEval := STRTRAN( cEval, "$SELECTOR$", cSelector )
    cEval := STRTRAN( cEval, "$VALUE$", cValue )    
    
    ::Eval( cEval )

RETURN NIL

METHOD Click( cSelector ) CLASS TAWaitWeb 
    LOCAL cEval := ""

    cEval := "document.querySelector('$SELECTOR$').click()"
    cEval := STRTRAN( cEval, "$SELECTOR$", cSelector )
    
    
    ::Eval( cEval )

RETURN NIL

Manual


Introduction

TAWaitWeb is a class designed for interacting with web elements within a TWebView control in a synchronous manner. It facilitates actions like querying, clicking, and modifying values of web elements. This manual provides an in-depth explanation of the class structure, methods, and their usage.

Class Definition
Class Name: TAWaitWeb
Class Purpose
The TAWaitWeb class enables developers to perform various actions on web elements within a TWebView control. It encapsulates JavaScript evaluations to ensure synchronous behavior and provides methods for different interactions with web elements.

Class Members
DATA bOldBind: A member variable to store the previous state of the binding status of the TWebView control.
DATA oWeb: A member variable to hold the TWebView control instance.
DATA cQuery: A member variable to store the query used for web element selection.
DATA nSecond: A member variable to define a timeout in milliseconds for waiting.
DATA cReturn: A member variable to store the response or result of actions performed within the class.

Constructor Method
Method Name: New( oWeb )
Purpose: Initializes an instance of the TAWaitWeb class.

Parameters:

oWeb: The TWebView control instance to be used for interactions.
Usage:

Action Methods
Method Name: Query( cQuery, nSecond )
Purpose: Performs a query on a web element using the provided selector and waits for a response.

Parameters:

cQuery: The CSS selector for the web element to be queried.
nSecond: (Optional) The timeout in milliseconds for waiting.

Method Name: QuerySelectAll( cQuery, nSecond )
Purpose: Performs a query to select multiple web elements using the provided selector and waits for a response.

Parameters:

cQuery: The CSS selector for the web elements to be queried.
nSecond: (Optional) The timeout in milliseconds for waiting.

Method Name: GetOuterHtml( cSelector, nSecond )
Purpose: Retrieves the outer HTML of a web element using the provided selector and waits for a response.

Parameters:

cSelector: The CSS selector for the web element.
nSecond: (Optional) The timeout in milliseconds for waiting.

Method Name: SetValue( cSelector, cValue )
Purpose: Sets a value on a specific web element using the provided selector.

Parameters:

cSelector: The CSS selector for the web element.
cValue: The value to be set on the web element.

Method Name: Click( cSelector )
Purpose: Simulates a click action on a specific web element using the provided selector.

Parameters:

cSelector: The CSS selector for the web element to be clicked.

Utility Methods
Method Name: GetBind( cJson, cCalls, … )
Purpose: Decodes JSON responses and sets the cReturn member variable.

Parameters:

cJson: The JSON response to be decoded.
cCalls: Additional parameters.
Usage: This method is used internally by other methods and doesn’t require direct user interaction.

Method Name: Eval( cEval )
Purpose: Evaluates JavaScript code within the TWebView control.

Parameters:

cEval: The JavaScript code to be evaluated.
Usage: This method is used internally by other methods and doesn’t require direct user interaction.

Conclusion
The TAWaitWeb class offers a comprehensive way to interact with web elements within a TWebView control using various action methods. Developers can use this class to perform queries, clicks, and value modifications on web elements, ensuring synchronous behavior. By following the provided usage examples, developers can easily integrate the TAWaitWeb class into their FiveWin projects and enhance their interactions with web elements.

Similar Posts

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다