Vba Excel 从 Internet Explorer 到 Selenium,转换代码

Vba Excel from Internet Explorer to Selenium, converting code

我的 VBA 代码直到今天都运行良好,现在 Internet Explorer 无法以正确的方式打开 url(我认为它太旧了)

Sub QUERYFUT()
On Error Resume Next
'query web future
Sheets("Foglio1").Activate
myURL = "https://www.barchart.com/futures/quotes/VI*0/futures-prices?timeFrame=daily"
Set IE = CreateObject("InternetExplorer.Application")
With IE
    .navigate myURL
    .Visible = True
    Do While .Busy: DoEvents: Loop    'Attesa not busy
    Do While .readyState <> 4: DoEvents: Loop 'Attesa documento
End With
myStart = Timer  
Do
    DoEvents
    If Timer > myStart + 5 Or Timer < myStart Then Exit Do
Loop
Sheets("Foglio1").Select
Set myColl = IE.document.getElementsByTagName("TABLE")
With myColl(0)
    For Each trtr In .Rows
        For Each tdtd In trtr.Cells
            'If J = 56 Then Exit For
            Cells(i + 1, J + 1) = tdtd.innerText
            J = J + 1
        Next tdtd
        i = i + 1: J = 0
DoEvents
    Next trtr
i = i + 1
End With
IE.Quit
Set IE = Nothing

我正在尝试让它与 selenium 和 chrome(firefox 也可以)一起工作

Option Explicit
Private cd As Selenium.ChromeDriver
Sub QUERYFUT()
On Error Resume Next
Sheets("Foglio1").Activate
Set cd = New Selenium.ChromeDriver
cd.Start
cd.Get "https://www.barchart.com/futures/quotes/VI*0/futures-prices?timeFrame=daily"

'Leggi le tabelle SUL FOGLIO ATTIVO
Sheets("Foglio1").Select
Dim myColl As Selenium.WebElements
Dim trtr As Selenium.WebElement
Dim tdtd As Selenium.WebElement
Dim J as Long
Dim i as Long
Set myColl = cd.FindElementsByTag("TABLE")
With myColl(0)
    For Each trtr In .Rows
        For Each tdtd In trtr.Cells
            'If J = 56 Then Exit For
            Cells(i + 1, J + 1) = tdtd.innerText
            J = J + 1
        Next tdtd
        i = i + 1: J = 0
    Next trtr
i = i + 1
End With
End Sub

这是我第一次使用 Selenium 和 Chrome,我做错了什么?

删除未关闭的 On Error Resume Next,它掩盖了所有潜在的错误。

乍一看,我可以看出您在 WebElement 上尝试使用 MSHTML.HTMLTable 的方法,但这行不通。

使用内置方法,例如Dim myColl As Selenium.Table然后用.ToExcel的方法一次性把table写出到指定范围


FireFox 不再受支持(从一个非常旧的版本开始就不再受支持)所以别管它了。您确实可以选择其他几种浏览器,但 Chrome 应该足够好了。

确保您的 Chrome 驱动程序和 Chrome 浏览器版本为 compatible