Jump to content

Cannot determine when page is fully loaded


Go to solution Solved by jasontj,

Recommended Posts

Posted

Hello all.  I have many scripts that I run against website to check for criminal records.  The one that is giving me trouble is the national sex offender database.  I cannot determine when the page is fully loaded.  It looks like they are using something like AJAX to load data from the backend and populate the page.   The looping won't work on the inserted code because I normally use an Excel spreadsheet.  Any help would be appreciated at determining when the page is fully loaded.  I put 5000ms on the "LoadWait", and also checked the HTML for specific text values.  Nothing works properly.

 

Thanks for taking a look.

Jason

 

#Region - include files
; non standard UDF's
#include "wd_helper.au3"
#include "wd_capabilities.au3"
#include <File.au3>
#include <Array.au3>
#EndRegion - include files

; Register a customer error handler
; _IEErrorHandlerRegister("MyErrFunc")

; If you want to download/update driver the next line should be uncommented'  Does it get latest verion, or just version to match your browser version?
_WD_UpdateDriver('chrome')

#Region - Global's declarations
Global Const $sElementSelector = "//input[@name='q']"
Global Const $aBrowsers[][2] = _
        [ _
        ["Chrome", SetupChrome] _
        ]

Global Const $aDebugLevel[][2] = _
        [ _
        ["None", $_WD_DEBUG_None], _
        ["Error", $_WD_DEBUG_Error], _
        ["Full", $_WD_DEBUG_Info] _
        ]

Global $sSession
Global $__g_idButton_Abort
#EndRegion - Global's declarations
$URLstr = "https://www.nsopw.gov/search-public-sex-offender-registries"
Global $sCheck


; RUN SEARCHES BELOW
Run_Pre_Screen($_WD_DEBUG_None, SetupChrome)

Exit

Func Run_Pre_Screen($idDebugging, $idBrowsers)
    ; Set debug level
    $_WD_DEBUG = $_WD_DEBUG_Info

    ; Execute browser setup routine for user's browser selection
    Local $sDesiredCapabilities = Call($idBrowsers)

    _WD_Startup()
    If @error <> $_WD_ERROR_Success Then Return

    $sSession = _WD_CreateSession($sDesiredCapabilities)
   _WD_Navigate($sSession, $URLstr)
   _WD_LoadWait($sSession, 1000)


    ; if Click Confirm button to dismiss dialog
    msgbox (0, "CLICK BUTTON", "Click the 'Continue' button if it appeared, and then click 'OK'")

; normally I would loop though values in my Excel file
   $INDEX = 1
   $sCellValueL = "SMITH"
   $sCellValueF = "JOHN"
   $RecordNumber = "XYZ"

   do
      ; Enter First Name Value
      $sElement = _WD_GetElementById($sSession, "firstname")
      If @error = $_WD_ERROR_Success Then
        _WD_ElementAction($sSession, $sElement, 'value', $sCellValueF)
      EndIf

      ; Enter Last Name Value
        $sElement = _WD_GetElementById($sSession, "lastname")
        If @error = $_WD_ERROR_Success Then
            _WD_ElementAction($sSession, $sElement, 'value', $sCellValueL)
        EndIf

      ; Click "Search" button
      $sButton = _WD_GetElementById($sSession, "searchbynamezip")


      _WD_ElementAction($sSession, $sButton, 'click')
      _WD_LoadWait($sSession, 5000)

      $sHTML = _WD_GetSource($sSession)
      $testStr3 = StringRegExp($sHTML, "Loading data. Please wait")

      if $testStr3 = 1 Then
        msgBox(0,"waiting", "Search taking too long.  Press 'OK' to continue")
        Sleep(10000)
      EndIf

    $sHTML = _WD_GetSource($sSession)
    $testStr3 = StringRegExp($sHTML, "Loading data. Please wait")

    if $testStr3 = 1 Then
        msgBox(0,"waiting", "Search taking too long.  Press 'OK' to continue")
        Sleep(10000)
    EndIf

    $sHTML = _WD_GetSource($sSession)
    $testStr1 = StringRegExp($sHTML, "Showing 0 to 0 of 0 entries")
    $testStr2 = StringRegExp($sHTML, "No data available in table")
    $testStr3 = StringRegExp($sHTML, "time out")

     if $testStr1 = 1 and $testStr2 = 1 Then
        ; Mark item 'clear' in my database


    Else
        _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, "POSSIBLE RECORD", "G" & $INDEX)
        $LastPossible = $RecordNumber
    EndIf


; Expand to search again  (COMMENTED OUT FOR NOW BECAUSE I AM TRYING A NEW ROUTE OF LOADING A WHOLE NEW PAGE TO SEE IF THIS HELPS
;        $sButton = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "/html/body/div/div[2]/main/div/div/div/div/article/div/div[2]/div/div/div/div/div[1]/h2/button")
;        $sButton = _WD_GetElementById($sSession, "nsopwsearchfilter")
;        If @error = $_WD_ERROR_Success Then
;           _WD_ElementAction($sSession, $sButton, 'click')
;           _WD_LoadWait($sSession, 2000)
;        EndIf

; Search from new page (LOAD WHOLE NEW PAGE)
      _WD_Navigate($sSession, $URLstr)
      _WD_LoadWait($sSession, 1000)

      $INDEX = $INDEX + 1
      $sCellValueL = "JOHNSON"
      $sCellValueF = "MARK"
      $RecordNumber = "XYZ-123"
      
      if INDEX = 2 then ; blank out cell value which will cause this to exit for loop (this is for testing only)
        $sCellValueL = ""
      EndIf

   until $sCellValueL = ""


if $LastPossible <> "" Then
    ; deal with last record
EndIf


_WD_Shutdown()
msgBox(0,"Finished", "All searches have been processed.")

EndFunc

 

Posted

@Danp2, thanks for replying.  I tried to use your suggestions, but I cannot determine what element to check for.  I opened the page (without performing a search), viewed the source of the page, saved it to a text file.  Then I performed a search with a random name that came back with 0 records, and ran a "FC" to compare the 2 text files.  I got "no differences".  Then I ran a search that came back with many records on a name of "ROBERT SMITH" and did the same thing.  The files still had no differences.  So, how do I determine where these results are being pulled into on this page?  Is it a frame of some sort?  I am at a loss.  Thanks.

C:\temp>fc sor1.txt sor2.txt
Comparing files SOR1.txt and SOR2.TXT
FC: no differences encountered
 

Jason

Posted

It sounds like you are using _WD_GetSource to retrieve the page's source code. This won't reflect any dynamically loaded elements. If you examine the source  code within the browser's Dev Tools (press F12 to open), then you should see the complete source.

I can see a Div element that appears to contain the spinning visual that is present when a search is active. Try using _WD_WaitElement to check for an xpath of "//div[@id='loaderGif']". Be sure to use the $_WD_OPTION_Hidden option so that it will only return once it is found and it isn't visible.

Posted


If you have frames try to use _WD_FrameList it will check each document (each separate frame) and will wait for they load.

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

  • Solution
Posted

Thanks @Danp2 and @mLipok,  I will give these suggestions a try today and post back the results.  I really appreciate you guys taking time to help me out.  

Here is an update.  This worked for me.  I did not have the "$_WD_OPTION_Hidden option".  I may need to update to get this function.  

_WD_WaitElement($sSession, $_WD_LOCATOR_ByXPath, "//div[@id='loaderGif']", 100, 2000, 1)
      $sElement1 = _WD_FindElement($sSession, $_WD_LOCATOR_ByXPath, "//div[@id='loaderGif']")
      $lIsVisible1 = _WD_ElementAction($sSession, $sElement1, 'displayed')

      $lIsVisible = _WD_ElementAction($sSession, $sElement1, 'displayed')
      do
         $lIsVisible = _WD_ElementAction($sSession, $sElement1, 'displayed')
      until $lIsVisible = False

I stored this variable ($lIsVisible1) immediately after I submit because sometimes the page has an error, and if $lIsVisible1 is false, then the spinner was never visible.  If this is the case, then I run the search again.  Overall, it seems to work.  I appreciate your help @Danp2.

 

Posted

I'm pretty bad about keeping things up to date.  If it's not broken, no need to download a new version..hahah..  I will download it tomorrow.  Thanks!

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...