Home > database >  chromedriver Selenium in vba
chromedriver Selenium in vba

Time:04-22

I am currently struggling with coding and need help from an expert. Help. There are a large number of classes ("df-table") on that link homepage. The first class was able to be entered into the excel sheet through various trials and efforts, but the second class ("df-table"), the third class ("df-table"), etc. are having a hard time figuring out what to do. I searched various internet resources, and they say that it can be solved by using nth-of-type or using xpath, but the error keeps occurring.

The results I want are the data values of the uploaded images.

I'm not a coding expert, so I'd appreciate it if you could consider that it may be difficult to understand on your own.

Public Sub Jaemu()
    
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    
    Dim d As WebDriver, ws As Worksheet, URL As String
    
    Set d = New ChromeDriver
    Set ws = ThisWorkbook.Worksheets("gemstone2")
        
    Dim http As New WinHttpRequest
    
    With d
        '.AddArgument "--headless"
        .Start "Chrome"
            
        Dim html As HTMLDocument
        Dim JsonObject As Object
        
        Set html = New HTMLDocument
        URL = "https://globalmonitor.einfomax.co.kr/infomax_ds.html#/USA/1/1"
        .get URL, Raise:=False  ' set raise to false to avoid a timeout error
        d.FindElementByCss("[ng-click='openStockSearchPopup();']").Click
        d.FindElementByCss("[ng-enter='searchStockSearchPopup(true);']").SendKeys "GOOGL"
        d.FindElementByCss("[ng-click='searchStockSearchPopup(true);']").Click
        d.FindElementByCss("[class='slick-cell l1 r1 text-center clickable']").Click
        Cells(2, 1).Value = d.FindElementByCss("[class='df-table']").Text
        Cells(3, 1).Value = d.FindElementByCss(".table-contents[ng-if='IS_RT_STATE_SUCCESS(requeststate.prospectData)'] > .df-table").Text

    End With
End Sub

I wanna these data in this picture.

enter image description here


Original OP error:

Problem coding line is this. runtime error 32.

Cells(3, 1).Value = d.FindElementByCss("[class='df-table' class:nth-of-type(2)]").Text

enter image description here


EDIT: New error following initial suggestion to use different CSS selector:

Problem coding line is this. Runtime error 7 .

Cells(3, 1).Value = d.FindElementByCss(".table-contents[ng-if='IS_RT_STATE_SUCCESS(requeststate.prospectData)'] > .df-table").Text

enter image description here

CodePudding user response:

Initial error (RTE32):

The :nth-of-type() pseudo class selector would go outside of the attribute selector closing ] i.e. "[class='df-table']:nth-of-type(2)", however this does not provide a match.

You can use:

.table-contents[ng-if='IS_RT_STATE_SUCCESS(requeststate.prospectData)'] > .df-table

This returns a single match and is specific enough, and not reliant on a long selector list, that it is likely fairly stable. Based on experience.

If you had wanted to use :nth-of-type() you could have used it on a parent element and taken first match, but that is less stable and prone to breaking if html changes:

.contents-area:nth-of-type(5) .df-table

Follow-up error (RTE7):

The element needs to be scrolled into view.

A not ideal way is (your mileage may vary):

d.ExecuteScript "window.scrollTo(0, document.body.scrollHeight/3);"
Application.Wait Now   TimeSerial(0, 0, 2)
Activesheet.Cells(1,3) = d.FindElementByCss(".table-contents[ng-if='IS_RT_STATE_SUCCESS(requeststate.prospectData)'] > .df-table").Text

There are better ways with inbuilt functions and custom JS which you can likely pull from the examples.xlsm by the author on GitHub. As I can only test with Python this was a quick and dirty test that worked.


You could avoid overhead of browser and use XHR to get a JSON response to then use a JSON parser with, as follows:

Option Explicit

Public Sub GetData()
    Dim s As String

    With CreateObject("MSXML2.XMLHTTP")
        .Open "POST", "https://globalmonitor.einfomax.co.kr/facset/getKeyData", False
        .SetRequestHeader "User-Agent", "Mozilla/5.0"
        .SetRequestHeader "content-type", "application/json;charset=UTF-8"
        .send "{""param"":""NAS:AAPL""}"
        s = .responsetext
    End With

   Debug.Print s

End Sub
  • Related