如何解决生成文件以供下载时的等待循环 - Selenium VBA
我正在尝试从站点自动下载文件。起点是一个页面,该页面已经有一个旧文件作为下载选项,我不想要它。我单击“导出”按钮,旧文件的下载选项消失,我得到一个微调器“创建文件”,持续一分钟。最后,新文件被创建并可供下载。
我可以编写代码来下载文件 OK 所以我知道可以下载文件。
我的等待循环尝试查找文件是否存在(通过搜索文件),如果没有找到,则继续查找,直到找到为止。一旦“创建文件”过程完成,我就无法再找到该文件,尽管我认为“地址”是正确的。错误说元素不可交互
在激活“创建文件”过程之前的 HTML:
<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:
单击“导出”按钮时,代码会发生变化,因此某些内容不会显示,可下载的(旧)文件被隐藏,并显示带有“正在创建文件”文本的微调器。
<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>
我想设置成当微调器正在旋转/正在创建文件时,系统会等待,并且仅在新文件/更新文件未隐藏时才进行下载。
我尝试了以下代码,该代码仅在 F8 步入时有效,但在离开时无效 - 尽管在 On Error GoTo 循环之前使用“地址”确实有效,但它会引发元素不可交互错误。它通过的方式看起来也很奇怪。它运行 SkipRetry 部分,然后进入 Retry 循环,执行 Resume,跳过 bot.Wait 5000,然后尝试单击所需的文件名,然后执行此操作,直到在下载时找到该文件 - 所以也许是我的语法错误。
我相信在 On Error Goto Retry 的第一步中正在识别该文件。我一直在尝试进行设置,以便在显示“创建文件”微调器时循环播放,但我无法进行任何操作。
'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
任何有关 Selenium VBA 语法的指针将不胜感激。谢谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。