If TimeOut

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
Timi
Newbie
Posts: 2
Joined: Wed Jul 22, 2020 3:18 pm

If TimeOut

Post by Timi » Wed Jul 22, 2020 3:27 pm

Hello, I have been writing a script to interact with a web page however, the web page always takes different time to load and sometimes it even gets stuck and never fully loads. To deal with different waiting times, I used the WaitScreenImage in my script. To deal with the second problem where it sometimes get stuck, I just used the TimeOut time. However, I was wondering if there is a way to have the script reload the page if it reaches the TimeOut time? So instead of just skipping the step, it would reload the page and wait for the image to appear again?

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1386
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: If TimeOut

Post by Dorian (MJT support) » Wed Jul 22, 2020 4:18 pm

I like to use IEWaitForText to check that a page has loaded.

Code: Select all

IEWaitForText>IE[0],,{"Log in"},0,20,result
If it times out, the result will be 0, so we can then refresh.

Depending on your application, the following structure may not be the best one for you. This simply uses a Goto. But the command examples will hopefully help.

//Example will find "macro recorder"

Code: Select all

IECreate>IE[0]
IENavigate>IE[0],https://www.mjtnet.com,navres

Label>TryAgain

IEWaitForText>IE[0],,macro recorder,0,10,TimeoutResult

If>TimeoutResult=0
  IERefresh>IE[0],res
  Goto>TryAgain
Endif

//Example will NOT find "QWERTYUIOP" and will keep refreshing the page.

Code: Select all

IECreate>IE[0]
IENavigate>IE[0],https://www.mjtnet.com,navres

Label>TryAgain

IEWaitForText>IE[0],,QWERTYUIOP,0,10,TimeoutResult

If>TimeoutResult=0
  IERefresh>IE[0],res
  Goto>TryAgain
Endif

This may be a tidier version. It creates an IE instance, and will re-navigate to the page in a loop if the text is not found. It may or may not be appropriate for your scenario :

Code: Select all

IECreate>IE[0]

Let>TimeoutResult=0
While>TimeoutResult=0
  IENavigate>IE[0],https://www.mjtnet.com,navres
  IEWaitForText>IE[0],,macro recorder,0,10,TimeoutResult
Endwhile
Yes, we have a Custom Scripting Service. Message me or go here

Timi
Newbie
Posts: 2
Joined: Wed Jul 22, 2020 3:18 pm

Re: If TimeOut

Post by Timi » Thu Jul 23, 2020 1:49 am

Hello Dorian, thanks for responding so quickly. This was pretty helpful and I tested it with some little tweaks and it worked, thank you. I was wondering if you have some ideas how I may approach this when using a different browser? I assume there is no built in command for that?

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1386
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: If TimeOut

Post by Dorian (MJT support) » Thu Jul 23, 2020 7:24 am

Hi, you're correct, those commands are just for IE.
Yes, we have a Custom Scripting Service. Message me or go here

kdtrog
Junior Coder
Posts: 43
Joined: Wed Mar 28, 2007 9:36 am

Re: If TimeOut

Post by kdtrog » Thu Jul 23, 2020 9:01 am

I was wondering if you have some ideas how I may approach this when using a different browser? I assume there is no built in command for that?
Here is a sample to wait for page load with Chrome or Edge.

Code: Select all

// Wait for a Web Page

Let>EDGEDRIVER_EXE=%script_dir%\msedgedriver.exe
Let>CHROMEDRIVER_EXE=%script_dir%\chromedriver.exe
Let>chrome=true
let>MyUrl=https://www.mjtnet.com/
let>timeout=30

//start a browser session
if>chrome=true
  ChromeStart>session_id
  ChromeNavigate>session_id,url,%MyUrl%
else
  EdgeStart>session_id
  EdgeNavigate>session_id,url,%MyUrl%
endif


//Wait until the Page is loaded
if>chrome=true
  while>timeout>0
    ChromeFindElements>session_id,tag name,nav,elements
    if>elements_count>0,loaded
    wait>1
    aub>timeout,1
  EndWhile
else
  while>timeout>0
    EdgeFindElements>session_id,"tag name",nav,elements
    if>elements_count>0,loaded
    wait>1
    sub>timeout,1
  EndWhile
endif
mdl>Web Page timeout
goto>quit_browser

label>loaded
mdl>Web Page loaded

//End the browser session
label>quit_browser
if>chrome=true
  ChromeQuit>session_id
else
  EdgeQuit>session_id
endif
exit

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts