从网站获取背景色 [英] get background color from website
问题描述
您好,我想从网站上获取背景色,以便将其与其他操作进行比较.就像网站的购买"按钮的背景色一样.
Hi im trying to get the backgound color from a website to compare it for other operations. Just like the Background color of the buy button of the web site.
Sub Colorfinder()
Dim oIE As New InternetExplorer
Dim oHtml As HTMLDocument
Dim tags As Object
Dim HTMLtags As IHTMLElementCollection
Dim HTMLtag As IHTMLElement
With oIE
.Visible = True
.navigate "https://www.gdax.com/trade/LTC-EUR"
Do Until .readyState = READYSTATE_COMPLETE: Loop
Set oHtml = .document
End With
'this is needed to wait until the page is totally loaded
Do: Set tags = oHtml.getElementsByClassName("OrderBookPanel_text_3fH-g")(0): DoEvents: Loop While tags Is Nothing
'getting the element that it is about
Do: Set HTMLtags = oHtml.getElementsByClassName("OrderForm_toggle_31S34"): DoEvents: Loop While HTMLtags.Length = 0
Set HTMLtag = HTMLtags(0).Children(0)
Debug.Print HTMLtag.innerText
'this is the problem
Debug.Print HTMLtag.Style.backgroundColor
End Sub
我从另一个拥挤的溢出用户那里得到了这个想法.但这没用. 链接停留溢出
i got this idea from an otherstuck overflow user. but it didn´t work. link stuckoverflow
推荐答案
尝试以下代码:
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文档中执行JScript代码,这就是为什么语法使用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.
这篇关于从网站获取背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!