0

I'm using Selenium VBA and Chrome to download a csv file.

On some slower PCs, the browser closes before the download is complete.

I've used bot.Timeouts.PageLoad = 500000 and bot.Timeouts.Server = 500000 (where I've declared 'bot' as New ChromeDriver earlier). That works when I need Chrome to wait until clicks on a link, finds an element, etc. but doesn't seem to have any affect when it comes to executing bot.Quit

Using the arbitrary bot.Wait (5000) works, but on some PCs it doesn't need to wait this long, and sometimes it needs to wait longer.

Is there any way I can get Chrome to wait until the download is complete before running bot.Quit?

Community
  • 1
  • 1
newuser2967
  • 316
  • 1
  • 4
  • 15
  • [Here](https://stackoverflow.com/questions/34548041/selenium-give-file-name-when-downloading/56570364#56570364) is the solutions provided in Java and Python selenium to make sure the script wait until the file download is completed. You can implement the solution in vba, let me know if you need any help. – supputuri Feb 10 '20 at 23:22
  • You mean this line of code? downloadPercentage = driver.execute_script( "return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('#progress').value") – newuser2967 Feb 11 '20 at 08:50
  • Yes, that line will get the % of download, you have to iterate until it's 100% or the max timeout is met. – supputuri Feb 11 '20 at 12:54
  • OK, how do I iterate until it reaches 100% then? – newuser2967 Feb 11 '20 at 12:59
  • Let me give me the snippet. – supputuri Feb 11 '20 at 13:05

1 Answers1

0

Here is the snippet to while loop.

Function getDownLoadedFileName(maxTimeInMins As int)
    Dim startTime As Date
    startTime = Now()
    Dim downloadPercentage As int
    Do While ElapsedTime(Now(),startTime) < maxTimeInMins 
         downloadPercentage = driver.execute_script( "return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('#progress').value")
        If (downloadPercentage = 100) Then
            getDownLoadedFileName = driver.execute_script("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('div#content  #file-link').text")
        End If
    Loop
End Function

Function ElapsedTime(endTime As Date, startTime As Date) 
    Dim Interval As Date 
    ' Calculate the time interval. 
    Interval = endTime - startTime 

    ' Format and return the time interval in seconds. 
    ElapsedTime = Int(CSng(Interval * 24 * 3600))
End Function
supputuri
  • 13,644
  • 2
  • 21
  • 39
  • I have not got a chance to execute the code because of my machine configuration. Please test the logic and let me know how it goes. – supputuri Feb 11 '20 at 13:37
  • OK... are you saying just paste this into my VBA code then...? – newuser2967 Feb 11 '20 at 13:55
  • You can call this function after imitating the download by clicking on the element. This function will make sure the download is completed before closing the browser/ your next line of code. – supputuri Feb 11 '20 at 13:57