If TimeOut
Moderators: Dorian (MJT support), JRL
If TimeOut
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?
- Dorian (MJT support)
- Automation Wizard
- Posts: 1380
- Joined: Sun Nov 03, 2002 3:19 am
- Contact:
Re: If TimeOut
I like to use IEWaitForText to check that a page has loaded.
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"
//Example will NOT find "QWERTYUIOP" and will keep refreshing the page.
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
IEWaitForText>IE[0],,{"Log in"},0,20,result
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
Re: If TimeOut
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?
- Dorian (MJT support)
- Automation Wizard
- Posts: 1380
- Joined: Sun Nov 03, 2002 3:19 am
- Contact:
Re: If TimeOut
Hi, you're correct, those commands are just for IE.
Yes, we have a Custom Scripting Service. Message me or go here
Re: If TimeOut
Here is a sample to wait for page load with Chrome or Edge.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?
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