Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.3k views
in Technique[技术] by (71.8m points)

Wait loop while file is generated for download - Selenium VBA

I am trying to automate download of files from a site. The starting point is a page which already has an old file present as an option to download which I don't want. I click the 'Export' button and the download option for the old file disappears and I get a spinner 'Creating file' which goes on for a minute plus. Finally the new file is created and available for download.

I can write code to download the file OK so I know that the file can be downloaded.

My wait loop tries to look for the presence of the file (by searching for the file) and if it does not find it carries on looking until it finds it. Once the 'Creating file' process has completed I can no longer find the file, although I think the 'address' is correct. The error says element not interactable

HTML BEFORE activating the 'Creating file' process:

<div class="bottom_sett-1">
                        <h4>Visitors</h4>
                        <form action="/admin/data/exportTask/Visitor" method="post">
                            <div class="bottom_sett-1__form exportForm" style="display: block;">                     
                            <span class="download" style="display: flex;">
                                <a href="/admin/data/downloadFile?f=https://xx/visitors-event-392.xlsx" style="display: block;">
                                    <i class="fa fa-download" aria-hidden="true"></i> Download .xlsx file (<span class="lastex">2021-01-12 09:40:49</span>)
                                </a>
                            </span>
                            <a href="javascript:void(0)" class="ex_ba export-btn active" id="exportVisitor">Export</a>
                            
                        </form>
                    </div>

HTML AFTER activating the 'Creating file' process:

When the 'Export' button is clicked the code changes so that some things are not displayed and the downloadable (old) file is hidden and a spinner with text 'Creating file' displays.

<div class="bottom_sett-1">
            <h4>Visitors</h4>
                <form action="/admin/data/exportTask/Visitor" method="post">
                      <div class="bottom_sett-1__form exportForm" style="display: **none**;">
                          <span class="download" style="display: none;">
                             <a href="/admin/data/downloadFile?f=https://expoplatform-files.s3-eu-west-1.amazonaws.com/api-obn.expoplatform.com/xx/visitors-event-392.xlsx" style="display: block;">
                                <i class="fa fa-download" aria-hidden="true"></i> Download .xlsx file 
                                     (<span class="lastex">2021-01-12 16:06:41</span>)
                              </a>
                           </span>
                            <a href="javascript:void(0)" class="ex_ba export-btn" 
                                  id="exportVisitor">Creating file <i class="fa fa-spinner fa-spin">
                                                                      ::before
                                                                    </i>
                             </a>
                           
                        </form>
                    </div>

I would like to set up so that when the spinner is spinning/the file is being created that the system waits and that the download occurs only when the new/updates file is not hidden.

I have tried the following code which will only work by F8 step into but not when left to run - it throws an element not interactable error despite the fact that the 'address' does work if used before the On Error GoTo loop. The way that it steps through looks strange as well. It runs the SkipRetry part then goes up to the Retry loop, does Resume, skips the bot.Wait 5000, then tries to Click on the the desired file name, and does this until the file is found when it does download - so perhaps my syntax is wrong.
I believe that the file is being recognised at the first step of On Error Goto Retry. I have been trying to set up so that it loops when the 'Creating file' spinner IS showing, but I cannot get anything to work.

'Start the export file creation process by clicking the 'Export' button

bot.FindElementByXPath("//*[@id='exportVisitor']").Click

bot.Wait 3000

' Wait Loop

On Error GoTo Retry
        
        bot.Wait 3000
        bot.FindElementByXPath ("//*[@id='main_content']/section/div[2]/div[1]/div[1]/div/form/span/a")
   ' this is the xlsx file to be downloaded (which is hidden-aria true when the spinner is spinning)

        GoTo SkipRetry

    Retry:
        bot.Wait 1000
        i = i + 1
        If i = 20 Then On Error GoTo 0
    Resume

    SkipRetry:

        bot.Wait 5000
        bot.FindElementByXPath("//*[@id='main_content']/section/div[2]/div[1]/div[1]/div/form/span/a").Click
    'Error throws here when not using Step through

Any pointers for Selenium VBA syntax would be very appreciated. Thanks.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...