Jump to content



Photo

Anyway To Exit A Loop Immediately?

exit loop end loop loop while loop

  • Please log in to reply
1 reply to this topic

#1 Denethor

Denethor

    Advanced Member

  • Members
  • PipPipPip
  • 50 posts
  • OS:Windows Vista
  • Total Memory:4Gb
  • Framework:v3.5 & v4.0
  • License:Professional Edition

Posted 19 April 2018 - 12:45 PM

I have many embedded loops to check for multiple possible errors. In each of those loops there are If/Then to detect which possible error it encountered and how to recover from that. To which the last part of the "then" needs to be stop executing this loop and go back to the last step. I see no way to do this in ubot. Other scripting apps I've used have commands like "Exit Loop" or "End Loop" to stop processing the rest of the loop.

If there are 10 possible errors and I find the error on the first try, why even try the other 9 and waste all that time?

The below is basically how it goes with most of the actual code removed and there are many more embedded loops  and if/thens to handle more possible errors, this just illustrated what I'm trying to accomplish.

Could the below be greatly simplified by making each "loop" its own subroutine/function? if so how is that done in Ubot?

 

Structure is like this:

 

set(#MainLoop,0,"Global")

Loop while(#MainLoop = 0) {

     ---login to site code here---

     set(#PageLoad,0,"Global")

     Loop while(#PageLoad = 0) {

           If error found

                then

                      ---clean up routine here---

                      set(#PageLoad,1,"Global")

           if error2 found

                then

                      ---clean up routine here---

                      set(#PageLoad,1,"Global")

     }

     set(#PageLoad,0,"Global") ---make sure the variable is reset before next loop---

    ---If we made it this far the page loaded correctly---

     ---wait for element on page ---

     ---Click save file button on page---

     save downloaded file(#filename)

     set(#FileSave,0,"Global")

     loop while(#FileSave,0,"Global"){

            set(#ButtonState,$scrape attribute(<id="export">,"outerhtml"),"Global")
            if($find index(#ButtonState,"data-style=\" data-loading=\"\">") < 0) {
                then {
                    ---text not found so ButtonState changed and file is ready to save---
                    if($file exists(#filename)) { ---checks to make sure the file from the above save actually got saved---
                        then {
                            comment("ADD DATA VERIFY CHECK HERE")
                            set(#FileSave,1,"Global")
                        }
                        else { ---Button state changed but file never got saved, what happened?---
                            set(#ErrorSeen,$scrape attribute(<class="error"),"Global")
                            if(#ErrorSeen = "There was a problem exporting your data") {
                                then { ---Reset Browser and load login page from table again---
                                    reset browser
                                    wait(5)
                                    ---NEEDS TO STOP LOOKING FURTHER HERE---
                                    ---Exit FileSave loop so it can process main loop again--
                                }
                                else {
                                }
                            }
                            set(#ErrorSeen,$scrape attribute($element offset(<tagname="h1">,0),"innertext"),"Global")
                            if(#ErrorSeen = "Log in") { 
                                then {---Login page re-appeared so session timed out---
                                    ---NEEDS TO STOP LOOKING FURTHER HERE---
                                    ---Exit FileSave loop so it can process main loop again--
                                }
                                else {
                                }
                            }
                            set(#ErrorSeen,$url,"Global")
                            if(#ErrorSeen = "https://www.site.com/error"){
                                then {---We ended up on custom error page try again---
                                    reset browser
                                    wait(5)
                                    ---NEEDS TO STOP LOOKING FURTHER HERE---
                                    ---Exit FileSave loop so it can process main loop again--
                                }
                                else {
                                }
                            }
                        }
                    }
                }
                else {---the button state is still greyed out which means data is loading still---
                    wait(7)---Wait 7 sec, loop back to beginning of FileSave loop---
                }
            }

      }

---We got here file must have saved correctly and passed verification---

increment(#LoginCount) ---Move to next record in table---

if($table cell(&Login,#LoginCount,0) = "") { ---Check to see if we've reached the end of the table---
     then {---we've process all records in table, clean up and move on to rest of the script---
           reset browser
           wait(5)
           set(#MainLoop,1,"Global")
     }
     else {---set variables for next cycle of MainLoop---
           set(#filename,"C:\\temp{$table cell(&Login,#LoginCount,2)}.csv","Global")
           set(#FileSave,0,"Global")
      }
}

}

---rest of script---



#2 Code Docta (Nick C.)

Code Docta (Nick C.)

    Member for Hire

  • Fellow UBotter
  • PipPipPip
  • 1475 posts
  • LocationIn a Python or UBot IDE or ubotdocta.com
  • OS:Windows 10
  • Total Memory:More Than 9Gb
  • Framework:v4.5+, unsure
  • License:Developer Edition

Posted 22 April 2018 - 05:18 AM

You can try using JavaScript nested as child loops and check for X on page.

 

run javascript command and $eval to return values.

 

 

Same with jquery. Just run minified jquery in run javascript commnand and return values with $eval

 

Regards,

CD


UBot Expert Tutorials Store

 

ubot-docta-header-ubot-expert-store.png

 

Python and UBot expert tutorials and documentation






Also tagged with one or more of these keywords: exit loop, end loop, loop while, loop

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users