简体   繁体   中英

Vba Excel from Internet Explorer to Selenium, converting code

I have this VBA code that was working fine until today, now Internet explorer is not opening the url in the right way (I think it is too old)

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

And I'm trying to make it working with selenium and chrome (firefox would be fine too)

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

It's my first time working with Selenium and Chrome, what I'm doing wrong?

Get rid of that unclosed On Error Resume Next which is masking all potential errors.

At first glance I can see you trying to use methods of MSHTML.HTMLTable on a WebElement , which won't work.

Use the in-built methods eg Dim myColl As Selenium.Table then use the .ToExcel method to write out the table to a specified range in one go.

在此处输入图片说明


FireFox is no longer supported (not since a very old version) so forget about that. You do have a choice of several other browsers but Chrome should be good enough.

Make sure your ChromeDriver and Chrome browser versions are compatible .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM