Vba 如何在关闭Chrome之前等待文件下载完成?
我正在使用Selenium VBA和Chrome下载csv文件 在一些速度较慢的电脑上,浏览器会在下载完成之前关闭 我使用了Vba 如何在关闭Chrome之前等待文件下载完成?,vba,selenium,google-chrome,selenium-chromedriver,Vba,Selenium,Google Chrome,Selenium Chromedriver,我正在使用Selenium VBA和Chrome下载csv文件 在一些速度较慢的电脑上,浏览器会在下载完成之前关闭 我使用了bot.Timeouts.PageLoad=500000和bot.Timeouts.Server=500000(我在前面声明“bot”为New ChromeDriver)。当我需要Chrome等待点击链接、查找元素等时,这种方法就可以工作,但在执行bot.Quit时似乎没有任何影响 使用任意的bot.Wait(5000)可以工作,但在某些PC上它不需要等待这么长时间,有时它
bot.Timeouts.PageLoad=500000
和bot.Timeouts.Server=500000
(我在前面声明“bot”为New ChromeDriver
)。当我需要Chrome等待点击链接、查找元素等时,这种方法就可以工作,但在执行bot.Quit时似乎没有任何影响
使用任意的bot.Wait(5000)
可以工作,但在某些PC上它不需要等待这么长时间,有时它需要等待更长的时间
有没有办法让Chrome等到下载完成后再运行bot.Quit
?这是while循环的代码片段
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
函数getDownLoadedFileName(maxTimeInMins为int)
暗淡的开始时间为日期
startTime=Now()
按整数显示的下载百分比
在ElapsedTime(Now(),startTime)
是Java和Python selenium提供的解决方案,用于确保脚本等待文件下载完成。你可以用vba实现这个解决方案,如果你需要帮助,请告诉我。你是说这行代码吗?downloadPercentage=driver.execute_script(“return document.querySelector('downloads-manager')).shadowRoot.querySelector('downloadsList downloads item')).shadowRoot.querySelector('#progress').value”)是的,该行将获得下载的百分比,您必须迭代,直到达到100%或满足最大超时。确定,我如何迭代直到它达到100%呢?让我给我一个代码片段。由于我的机器配置,我没有机会执行代码。请测试一下逻辑,让我知道是怎么回事。好的。。。你是说只要把它粘贴到我的VBA代码中…?你可以在模拟下载后通过点击元素调用这个函数。此功能将确保在关闭浏览器/下一行代码之前完成下载。