我有一个代码,可让我使用凭据登录网站。为了保护隐私,我在代码中将它们替换为hidden1,hidden2和hidden3。 代码登录到网站,然后导航到具有两个下拉列表的特定页面。 从具有四个选项(固定为四个选项)的第一个列表中,选择一个选项,然后根据list1更新第二个下拉列表 但是List2是可变的,因为选项未知 下一步是单击一个按钮,该按钮将转到包含我单击的两个元素的另一页,以便导出pdf文件。 到目前为止一切都很好。
问题是我已经等待15秒钟,以便等待下载,我正在寻找更有效的方法来检查文件是否使用硒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硒的链接。而且我需要处理VBA硒。 问候
答案 0 :(得分:3)
您可以在下载过程代码之前在DOWNLOAD_DIRECTORY中检查文件计数,然后在下载过程代码之后再次检查。如果计数增加一,则将其下载。 只是一个想法
我找到了一个用来计算出勤文件的代码。您可以利用它
Sub Attfiles()
Dim AttFolder As String, path As String, count As Integer
AttFolder = "D:\attdata"
path = AttFolder & "\*.xls" 'ممكن تغير الامتداد هنا
Filename = Dir(path)
Do While Filename <> ""
count = count + 1
Filename = Dir()
Loop
Range("A5").Value = count
End Sub