VBA 网页日历中 Select 日期的代码

VBA code to Select Date from Calendar on Webpage

我想在 select 之前从休假 link 下载 excel https://www.mcxindia.com/market-operations/clearing-settlement/daily-margin.

我试过休假 VBA 代码

Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate "https://www.mcxindia.com/market-operations/clearing-settlement/daily-margin"
Loop While ie.ReadyState <> 4 Or ie.Busy = True
ie.document.getelementbyid("txtDate").Value = "14/02/2022"
ie.document.getelementbyid("txtDate").Focus
ie.document.getelementbyid("txtDate").Selected = True
ie.document.getelementbyid("txtDate").Click

请澄清我的错误,这有助于我 select 约会并下载 csv 文件

霍拉·阿希什, 我会帮助你,因为至少你尝试过,为了获得对对象的更多控制并做你需要的,我使用 Selenium Driver 在 Google Chrome 中使用,你需要根据需要下载驱动程序从网站 chrome 到您的版本:https:/ /chromedriver.chromium.org/downloads。 以及使它工作的 Selenium 驱动程序...安装 exe。 SeleniumBasic v2.0.9.0: https://github.com/florentbr/SeleniumBasic/releases/tag/v2.0.9.0

使用 'Selenium.ChromeDriver' 转到:工具 > 参考 ... 添加 ... 在安装 Selenium 驱动程序的文件夹中,还将文件 chromedriver.exe 指向文件:'Selenium64.tlb' 或 32.

我需要找到日期以避免许多错误和其他错误可能出现,特别是如果等待不是对象可用所需的时间,请使用 debug.print 并在出现问题时进行调试。 不要忘记将 chrome 驱动文件放在 selenium 安装文件夹中,并检查你的文件夹 'C:\Users\Your_User\AppData\Local\SeleniumBasic' 它通常安装在那里。

如果对回答有帮助。

Option Explicit
Private ch As Selenium.ChromeDriver

Public Function GET_DailyMargin()
On Error GoTo Err_Control
Dim FindBy As New Selenium.By
Dim DtDate As Date
Dim WeekOfMonth As Long
Dim DayOfMonth As Long
Dim DayOfFirst As Integer
Dim StartOfWeek2 As Integer
Dim weekNum As Integer

DtDate = Date

'DtDate = "08/02/2022"

If Weekday(DtDate) = 1 Then 'vbSunday
    DayOfMonth = 1
ElseIf Weekday(DtDate) = 2 Then 'vbMonday
    DayOfMonth = 2
ElseIf Weekday(DtDate) = 3 Then 'vbTuesday
    DayOfMonth = 3
ElseIf Weekday(DtDate) = 4 Then 'vbWednesday
    DayOfMonth = 4
ElseIf Weekday(DtDate) = 5 Then 'vbThursday
    DayOfMonth = 5
ElseIf Weekday(DtDate) = 6 Then 'vbFriday
    DayOfMonth = 6
ElseIf Weekday(DtDate) = 7 Then 'vbSaturday
    DayOfMonth = 7
End If
''Discovery week of mounth
''Credits: -> 
DayOfFirst = Weekday(DateSerial(Year(DtDate), Month(DtDate), 1), vbSunday)
StartOfWeek2 = (7 - DayOfFirst) + 2
    Select Case DtDate
        Case DateSerial(Year(DtDate), Month(DtDate), 1) _
        To DateSerial(Year(DtDate), Month(DtDate), StartOfWeek2 - 1)
            WeekOfMonth = 1
        Case DateSerial(Year(DtDate), Month(DtDate), StartOfWeek2) _
        To DateSerial(Year(DtDate), Month(DtDate), StartOfWeek2 + 6)
            WeekOfMonth = 2
        Case DateSerial(Year(DtDate), Month(DtDate), StartOfWeek2 + 7) _
        To DateSerial(Year(DtDate), Month(DtDate), StartOfWeek2 + 13)
            WeekOfMonth = 3
        Case DateSerial(Year(DtDate), Month(DtDate), StartOfWeek2 + 14) _
        To DateSerial(Year(DtDate), Month(DtDate), StartOfWeek2 + 20)
            WeekOfMonth = 4
        Case DateSerial(Year(DtDate), Month(DtDate), StartOfWeek2 + 21) _
        To DateSerial(Year(DtDate), Month(DtDate), StartOfWeek2 + 27)
            WeekOfMonth = 5
        Case DateSerial(Year(DtDate), Month(DtDate), StartOfWeek2 + 28) _
        To DateSerial(Year(DtDate), Month(DtDate) + 1, 1)
            WeekOfMonth = 6
    End Select

Set ch = New Selenium.ChromeDriver 'if comment this line to ch.Get, not open again, discomment DtDate here and you get any date without having to reopen the browser
ch.SetPreference "plugins.always_open_pdf_externally", True
ch.SetPreference "download.prompt_for_download", False
ch.SetPreference "browser.download.folderList", 2
ch.Start
ch.Get "https://www.mcxindia.com/market-operations/clearing-settlement/daily-margin"

ch.Refresh
ch.Window.Maximize
ch.FindElementById("txtDate").SendKeys DtDate
ch.FindElementById("txtDate").Click
ch.Wait (1000)
    If Format((ch.FindElement(FindBy.XPath("/html/body/div[2]/div/div[2]/div/table/tbody/tr[" & WeekOfMonth & "]/td[" & DayOfMonth & "]/a")).Text), "00") = Format(DtDate, "dd") Then
        ch.FindElement(FindBy.XPath("/html/body/div[2]/div/div[2]/div/table/tbody/tr[" & WeekOfMonth & "]/td[" & DayOfMonth & "]/a")).Click
        ch.FindElementById("btnShow").Click
    Else
        Debug.Print ch.FindElement(FindBy.XPath("/html/body/div[2]/div/div[2]/div/table/tbody/tr[" & WeekOfMonth & "]/td[" & DayOfMonth & "]/a")).Text
        MsgBox "Check the date, or web page updates", vbOKOnly, "Error"
    End If

ch.Wait (3000)
ch.FindElementById("cph_InnerContainerRight_C001_lnkExportToCSV").Click
ch.Wait (3000)
ch.Close
    
Err_Control:
If Err.Number <> 0 Then
    If Err.Number = 7 Then
        Resume Next
        Err.Clear
    ElseIf Err.Number = 11 Then
        MsgBox "download not available, verify"
        Err.Clear
        Stop
        Exit Function
    End If
    MsgBox Err.Description
End If
End Function