且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从网站获取背景色

更新时间:2023-12-04 19:48:28

尝试以下代码:

Sub Colorfinder()

    With CreateObject("InternetExplorer.Application")
        .Visible = True
        .navigate "https://www.gdax.com/trade/LTC-EUR"
        Do While .Busy Or .readyState < 4: DoEvents: Loop
        With .Document
            Do While .getElementsByClassName("OrderBookPanel_text_3fH-g").Length = 0: DoEvents: Loop
            Do While .getElementsByClassName("OrderForm_toggle_31S34").Length = 0: DoEvents: Loop
            With .parentWindow
                .execScript "var e = document.getElementsByClassName('OrderForm_toggle_31S34')[0].children[0];"
                .execScript "e.style.backgroundColor = window.getComputedStyle(e,null).getPropertyValue('background-color');"
            End With
            Debug.Print .getElementsByClassName("OrderForm_toggle_31S34")(0).Children(0).Style.backgroundColor ' rgb(77, 165, 60)
        End With
    End With

End Sub

.execScript在HTML文档中执行JScr​​ipt代码,这就是为什么语法使用var'[];等的原因,变量e是文档范围中声明的目标节点,并且它的实际计算出的背景值已放入.backgroundColor属性中.我发现使用.execScript是进入window.getComputedStyle方法的唯一途径,而window.getComputedStyle方法实际上可以完成工作.

.execScript executes JScript code within HTML document, that is why syntax uses var, ', [] and ;, etc., variable e is the target node declared in the document scope, and it's actual computed background value just put into .backgroundColor property. Using .execScript is the only way I found to get to window.getComputedStyle method, which actually do the job.