我想通过选中网页上的框来获取数据

时间:2018-12-08 20:32:28

标签: excel vba excel-vba

我想从网站上获取特定日期范围内的数据。该站点地址是“ http://arsiv.mackolik.com/Canli-Sonuclar”。此地址中的某些框应选中或不选中。从这些框中选择“ Futbol(足球文本)”,“ Basketbol(篮球文本)”。将选择“ TariheGöre(aOrderBydate)”。但是不会标记“ Duello(chkDuel)”,“ Iddaa(chkIddaa)”,“Canlı(chkLive)”和“Seçili(chkSelected)”。根据这些首选项,我希望将“列表表”表中的所有数据与链接地址一起传递。例如,我将一次性获取一个月的数据并将其传输到excel工作表。我尚未编写代码或更改代码。

1 个答案:

答案 0 :(得分:0)

以下将进行选择;有javascript允许等待元素可点击;进行您指定的取消选择;并向您展示如何以一天的增量将日期改回。

Option Explicit

Public Sub MakeSelections()
    Dim d As WebDriver, t As Date, ws As Worksheet, i As Long, table As Object
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    Const MAX_WAIT_SEC As Long = 10
    Const URL = "http://arsiv.mackolik.com/Canli-Sonuclar#"

    Const JS_WAIT_CLICKABLE = _
    "var target = this, endtime = Date.now() + arguments[0];" & _
    "(function check_clickable() {" & _
    "  var r = target.getBoundingClientRect(), x = r.left+r.width/2, y = r.top+r.height/2;" & _
    "  for (var e = document.elementFromPoint(x , y); e; e = e.parentElement)" & _
    "    if (e === target){ callback(target); return; }" & _
    "  if (Date.now() > endtime) { callback(target); return; }" & _
    "  setTimeout(check_clickable, 60);" & _
    "})();"                                      'by @florentbr

    Set d = New ChromeDriver

    With d
        .Start "Chrome"
        .get URL
        .Window.Maximize

        With .FindElementByCss("[data-cc-event='click:dismiss']")
            .ExecuteAsyncScript(JS_WAIT_CLICKABLE, 3000) _
        .Click
        End With
        .FindElementByCss("#chkSport1").ScrollIntoView

        With .FindElementByCss("#chkSport1")
            If Not InStr(.Attribute("class"), "selected") > 0 Then
                .Click
            End If
        End With
        With .FindElementByCss("#chkSport2")
            If Not InStr(.Attribute("class"), "selected") > 0 Then
                .Click
            End If
        End With
        With .FindElementByCss("#aOrderByDate")
            .ExecuteAsyncScript(JS_WAIT_CLICKABLE, 3000) _
            .Click
        End With
        With .FindElementByCss("#chkDuel")
            If InStr(.Attribute("class"), "selected") > 0 Then
                .Click
            End If
        End With

        With .FindElementByCss("#chkIddaa")
            If InStr(.Attribute("class"), "selected") > 0 Then
                .Click
            End If
        End With
        With .FindElementByCss("#chkLive")
            If InStr(.Attribute("class"), "selected") > 0 Then
                .Click
            End If
        End With
        With .FindElementByCss("#chkSelected")
            If InStr(.Attribute("class"), "selected") > 0 Then
                .Click
            End If
        End With

        Set clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
        'go back a day at a time.Add a loop here to change multiple days.
        For i = 1 To 2
            If i > 1 Then
                .FindElementByCss("[onclick='gotoDate(-1);']").Click
            End If
            t = Timer
            Do
                DoEvents
                On Error Resume Next
                Set table = .FindElementByCss(".list-table")
                If Timer - t > MAX_WAIT_SEC Then Exit Do
                On Error GoTo 0
            Loop While table Is Nothing
            'other code
        Next

        Stop                                     '<== Delete me later

        .Quit
    End With
End Sub