Results 1 to 17 of 17

Thread: Loop until element exists, the continue execution

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2013
    Posts
    21

    Loop until element exists, the continue execution

    I have a long script (VB6) that fills in a form.

    In a certain part, the user must import a file and must click "Next" to continue.

    I would like to wait for this user input and then continue executing the script.

    When the user clicks "Next", a new page will appear and there are some unique elements. I want to continue executing the script based on the existence of one of these elements.

    I have been reading about using Do..Events... but some others suggest a combination with Sleep.

    Please advise what would be the best?

    Thanks!

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Loop until element exists, the continue execution

    Quote Originally Posted by slowdive View Post
    Please advise what would be the best?
    It would be best to avoid using DoEvents (or variations of it) in a busy wait loop. You'll be better off redesigning your code so that your program doesn't loop in order to wait. Try searching this forum for the many reasons of why DoEvents is "evil".

    Can you describe or post your current code? Someone might be able to suggest a better approach than what you have now.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,263

    Re: Loop until element exists, the continue execution

    Sounds like the usual "must-be-filled" validation. I always do it in the way, that as long as the "must"-data is not filled, the user cannot click on "next"
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Loop until element exists, the continue execution

    I would like to wait for this user input and then continue executing the script.
    use your own input form or dialog, then fill the required element with input information and submit the form

    if it is a file input that can not be automated, i recently posted a http method to upload files, using the information in the file input form, that you can try, if you wish to try and can not find the example post back and i will find the thread it is in
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Loop until element exists, the continue execution

    If I understand correctly, you wish to suspend execution until the user has performed some operation involving a file. Rather than use a command 'Next' why not just prompt the user for the information when it is required. Using a simple MsgBox (or InputBox or a CommonDialog) will suspend execution until the user responds.
    e.g.
    Code:
    .. blah
    .. blah
    lngReturn = MsgBox("Please import the file now, then click OK to continue", vbOkCancel, "Waiting for User")
    If lngReturn = vbOk Then
        .. blah
        .. blah
    Else
       <user clicked Cancel>
    End If

  6. #6
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    813

    Re: Loop until element exists, the continue execution

    "DoEvents is Evil" ...

    "Type Coercion is Evil" ...

    "End is Evil" ...



    I miss VB "Proper"!
    Bwah hah hah !!

    Regards, Phill W.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Feb 2013
    Posts
    21

    Re: Loop until element exists, the continue execution

    Quote Originally Posted by Doogle View Post
    If I understand correctly, you wish to suspend execution until the user has performed some operation involving a file. Rather than use a command 'Next' why not just prompt the user for the information when it is required. Using a simple MsgBox (or InputBox or a CommonDialog) will suspend execution until the user responds.
    e.g.
    Code:
    .. blah
    .. blah
    lngReturn = MsgBox("Please import the file now, then click OK to continue", vbOkCancel, "Waiting for User")
    If lngReturn = vbOk Then
        .. blah
        .. blah
    Else
       <user clicked Cancel>
    End If
    this does the job BUT the problem is that the user will have to move/hide this popup window while he imports the file(s) and check if all info is OK. then he will have to get back to the popuwindow and click OK to continue the process. it's possible to do the same thing without suspending the execution with a popup window?

  8. #8
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Loop until element exists, the continue execution

    I think you should be able to do what you want by creating your own dialog and showing it as a modal form. See if something like the attached would work for you.
    Attached Files Attached Files

  9. #9
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Loop until element exists, the continue execution

    Quote Originally Posted by slowdive View Post
    this does the job BUT the problem is that the user will have to move/hide this popup window while he imports the file(s) and check if all info is OK. then he will have to get back to the popuwindow and click OK to continue the process. it's possible to do the same thing without suspending the execution with a popup window?
    Not sure I understand your logic....

    You have some process running and at some point in time you require the user to do somthing (i.e. Download a file). Whilst they are doing that your program cannot proceed.
    Given that, why would you want your program to 'continue' (to do what ?) Perhaps you need to explain exactly what's going on.

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Feb 2013
    Posts
    21

    Re: Loop until element exists, the continue execution

    Quote Originally Posted by Doogle View Post
    Not sure I understand your logic....

    You have some process running and at some point in time you require the user to do somthing (i.e. Download a file). Whilst they are doing that your program cannot proceed.
    Given that, why would you want your program to 'continue' (to do what ?) Perhaps you need to explain exactly what's going on.
    OK, let's say the user has to fill 3 web forms and each form is one page.

    1) in the first page, the script fills in the form and then click the button "Next" (all data is already present in txt files)

    2) the second page loads. in this page, the user must import a file. this step will be manual. therefore, the program must "paused" and wait for the user to import the file and click in the button "Next"

    3) the third page shows up. the script should be able to recognize that we're now in the 3rd page (this can be done by identifying for example if an element exists?) and should therefore fill in the form in this page automatically.

    to summarize: we have 3 forms located in 3 different pages (don't ask me to change that because that's how the application is set). form of page 1 is filled in by the script, form of page 2 (importing a file) will be doing manually. form of page 3 should be filled also by the script.

    thanks in advance

  11. #11
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Loop until element exists, the continue execution

    OK. Your first post didn't mention 'Web Forms'

    So what are you trying to do ? Automate some sort of Web application or what ?

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Feb 2013
    Posts
    21

    Re: Loop until element exists, the continue execution

    Quote Originally Posted by Doogle View Post
    OK. Your first post didn't mention 'Web Forms'

    So what are you trying to do ? Automate some sort of Web application or what ?
    yes. i am automating a web application. specifically, parts where user must fill in some info in a web form. what I am struggling at is how to do to wait for the user to complete the manual task in the process (import the files) and then continue the automatic process.

    i know can do this by showing a msgbox as someone suggested before (the script won't continue until the user does not click "OK to continue") but I want a wait to do the same but without interaction with a msgbox. is this possible? if yes, how?

    thanks for your help.

  13. #13
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Loop until element exists, the continue execution

    Can you post or attach your code?
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Feb 2013
    Posts
    21

    Re: Loop until element exists, the continue execution

    here it's. thanks!

    Code:
    	''First part	
    
    	'click on link1
    	objIE.document.getElementById("link1").click
    
    	'click on link2
    	objIE.document.getElementById("link2").click
    
    	'Wait for user action
    	'In this part of the process, the user must browse and select a file, and then must check the file's content (which will be shown on the page)
            'Therefore this part must be manual
    
            'We show a popup to wait for the user input
            'Once the user has uploaded and checked the file's content, he should click Next
            'Unfortunately, the MsgBox is intrusive. Is there a way to achieve the same without a MsgBox?
    
    	lngReturn = MsgBox("Please import the file now, then click OK to continue", vbOkCancel, "Waiting for User")
    
    	If lngReturn = vbOK Then
    
    		'Click Next
    		objIE.document.getElementById("Next").click
    
    		'Wait For the page To load
    		While objIE.Busy Or objIE.readyState <> READYSTATE_COMPLETE
    			DoEvents
    		Wend
    
    		'Select the Customer type
    		 objIE.document.getElementById("customerType").value = "2"
    
    		 objIE.document.getElementById("ctl00_ContentPlaceHolderAuthenticated_DropDownListCustomerFilterBy").FireEvent("onchange")
    
     		'Wait For the page To load
    		While objIE.Busy Or objIE.readyState <> READYSTATE_COMPLETE
    			DoEvents
    		Wend
    
    		'Fill in the Customer id
    		objIE.document.getElementById("customerID").value = "777"
    
    	
    		'Click the Search button
    		objIE.document.getElementById("search").click
    
    	Else
    
    		MsgBox ("Execution ended")
    
    	End If

  15. #15
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Loop until element exists, the continue execution

    if you click the browse button of the file input with code, the code will pause until the user selects a file (or cancels), then continue
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  16. #16
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Loop until element exists, the continue execution

    Not too sure about this.

    Does the user navigate using the browser that you have automated or do they start up another browser session?
    If the former, is the 'Next' button on the page the user has ended up at or is it on the original page you were on before they did any manual things?

    Is the file in question uploaded by the user to the web page or is it download by the user from the web page?

    The scenario begs the question, "Why doesn't the user do all this before using your application?"

  17. #17
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Loop until element exists, the continue execution

    The scenario begs the question, "Why doesn't the user do all this before using your application?"
    my guess he wants to fully automate, but is unable to upload the file by code, so needs to pass back to user to select in the file dialog, then continue the automation
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width