0

我尝试了下面的代码。大部分时间跑步都卡在了后面on error goto 0,没有成功.fireevent ("onchange")。它可能是某种方式可以更好地优化流程。请帮帮我,谢谢。

Public Sub makeselections()

Dim ie As New InternetExplorer, var As String, ele As Object

var = ThisWorkbook.Worksheets("Sheet2").Cells(1, 1).value

With ie
.Visible = True
.Navigate2 "https://www.marketwatch.com/investing/stock/" & var & "/financials"

While .Busy Or .readyState < 4: DoEvents: Wend

With .document
  .querySelector("#autocomplete_input").value = var
  .querySelector("#investing_ac_button").Click
End With

While .Busy Or .readyState < 4: DoEvents: Wend

With .document
Do
 On Error Resume Next
   Set ele = .querySelector("[value^='/investing/stock/" & LCase(var) & "/financials/Income/quarter']")
 On Error GoTo 0

 Loop While ele Is Nothing
   .querySelector("[value^='/investing/stock/" & LCase(var) & "/financials/Income/quarter']").Selected = True
   .querySelector(".financials select").FireEvent "onchange"
 End With

 End With

End Sub
4

1 回答 1

0

它可能被卡住了,因为 ele 仍然什么都没有,即找不到ticker,或者至少找不到 href 值。使用定时循环允许退出

Option Explicit
Public Sub MakeSelections()
    Dim ie As New InternetExplorer, var As String, ele As Object, t As Date
    Const MAX_WAIT_SEC As Long = 10

    var = ThisWorkbook.Worksheets("Sheet2").Cells(1, 1).Value

    With ie
        .Visible = True
        .Navigate2 "https://www.marketwatch.com/investing/stock/" & var & "/financials"

        While .Busy Or .readyState < 4: DoEvents: Wend

        With .document
            .querySelector("#autocomplete_input").Value = var
            .querySelector("#investing_ac_button").Click
        End With

        While .Busy Or .readyState < 4: DoEvents: Wend

        With .document
            t = Timer
            Do
                On Error Resume Next
                Set ele = .querySelector("[value$='quarter']")
                On Error GoTo 0
                If Timer - t > MAX_WAIT_SEC Then Exit Do
            Loop While ele Is Nothing
            If ele Is Nothing Then Exit Sub
            ele.Selected = True
            .querySelector(".financials select").FireEvent "onchange"
        End With
    End With
End Sub
于 2019-10-10T06:59:31.970 回答