【发布时间】:2018-12-25 06:40:07
【问题描述】:
我有一个代码,可以让我使用凭据登录网站。为了隐私,我在代码中用 hidden1、hidden2 和 hidden3 替换了那些。 代码登录到网站,然后导航到具有两个下拉列表的特定页面。 从具有四个选项的第一个列表(固定为四个选项)中,我选择一个选项,然后根据 list1 更新第二个下拉列表 但 List2 是可变的,因为选项未知 下一步是单击一个按钮,该按钮转到另一个页面,其中包含我单击的两个元素,以便导出 pdf 文件。 到目前为止一切都很好。
问题是我已将等待线等待 15 秒以等待下载,我正在寻找更有效的方法来检查文件是否已使用 selenium vba 下载。
这是代码
Private Sub Test()
Dim bot As New Selenium.WebDriver
Dim sList1 As SelectElement
Dim sList2 As SelectElement
Dim By As New By
Dim fso As Object
Dim myFolder As Object
Dim objFile As Object
Dim fil As String
Dim fn As String
Dim dteFile As Date
Dim n As Integer
Dim x As Integer
Dim j As Integer
Const DOWNLOAD_DIRECTORY As String = "C:\Users\Future\Desktop\Files"
Set fso = CreateObject("Scripting.FileSystemObject")
If Len(Dir(DOWNLOAD_DIRECTORY, vbDirectory)) = 0 Then MkDir DOWNLOAD_DIRECTORY
With bot
.SetPreference "download.default_directory", DOWNLOAD_DIRECTORY
.SetPreference "download.directory_upgrade", True
.SetPreference "download.prompt_for_download", False
.Start "chrome", "http://primprep.emis.gov.eg"
.Get "/"
.FindElementById("ContentPlaceHolder1_TextBox1").SendKeys "hidden1"
.FindElementById("ContentPlaceHolder1_TextBox3").SendKeys "hidden2"
.FindElementById("ContentPlaceHolder1_TextBox2").SendKeys "hidden3"
.FindElementById("ContentPlaceHolder1_Button2").Click
.FindElementById("Button1").Click
.Wait 1000
mLoop:
n = n + 1
If n = 5 Then Stop
x = 0
sPoint:
Set sList1 = .FindElementById("ContentPlaceHolder1_Dedara").AsSelect
sList1.SelectByIndex n
.Wait 2000
Set sList2 = .FindElementById("ContentPlaceHolder1_Dschool").AsSelect
For j = x + 1 To sList2.Options.Count
If x + 1 >= sList2.Options.Count Then GoTo mLoop
fil = Format(n, "00") & "-" & Format(j, "00") & "-" & Application.Trim(sList2.Options(j + 1).Text) & ".pdf"
sList2.SelectByIndex j
.FindElementById("ContentPlaceHolder1_Button1").Click
.Wait 2000
If .IsElementPresent(By.ID("ContentPlaceHolder1_Label2")) Then
If .FindElementById("ContentPlaceHolder1_Label2").Text = "لا يوجد بيانات لعرضها" Then
Debug.Print "No Data For This School >> " & Application.Trim(Replace(fil, ".pdf", ""))
x = x + 1
GoTo sPoint
End If
End If
Do
Loop While .FindElementsById("IconImg_CrystalReportViewer1_toptoolbar_print").Count = 0
.FindElementById("IconImg_CrystalReportViewer1_toptoolbar_print").Click
Do
Loop While .FindElementsByCss("[id^='theBttnbobjid']").Count = 0
.FindElementByCss("[id^='theBttnbobjid']").Click
Application.Wait Now + TimeSerial(0, 0, 15)
'I need a way to check if the file downloaded or not instead of waiting for 15 seconds
'as sometimes the file took no time and sometimes the file may took over 15 seconds
Set myFolder = fso.GetFolder(DOWNLOAD_DIRECTORY)
dteFile = DateSerial(1900, 1, 1)
For Each objFile In myFolder.Files
If objFile.DateLastModified > dteFile And fso.GetExtensionName(objFile.Path) = "pdf" Then
dteFile = objFile.DateLastModified
fn = objFile.name
End If
Next objFile
If fn <> vbNullString And Not fso.FileExists(DOWNLOAD_DIRECTORY & "\" & fil) Then
fso.MoveFile DOWNLOAD_DIRECTORY & "\" & fn, DOWNLOAD_DIRECTORY & "\" & fil
End If
.FindElementById("ContentPlaceHolder1_Button2").Click
.Wait 2000
x = x + 1
GoTo sPoint
Next j
GoTo mLoop
End With
End Sub
我已经搜索过这样的主题并找到了一个链接,但它是针对 Java selenium 的。我需要处理 VBA 硒。 问候
【问题讨论】:
标签: excel vba selenium-webdriver web-scraping