如何获取确定网页的特定元素以在excel WebBrowser中显示它,例如<img>
或<iframe>
或<audio>
或<a>
等等
我知道如果图像不是网页的一部分,如何显示图像。
WebBrowser1.Navigate "about:blank"
WebBrowser1.Document.Write "<img style=""width:100%;"" src=""http://www.sthua.edu.sg/images/vba-logo.jpg"">"
但我不知道如何从任何网页获取id或类名的元素。
答案 0 :(得分:0)
以下示例说明如何从the webpage translate.google.com.eg检索class="gt-baf-table"
元素,并将其放入WebBrowser1
的{{1}}控件中:
UserForm1
Option Explicit
Sub Test()
Dim objIE As Object
Dim objNode As Object
Dim objTable As Object
Dim strHtmlContent As String
Dim colSSheets As Object
Dim objSSContent As Object
Dim varSSheet
Dim objWB As Object
Dim objHead As Object
Dim varCssNumber
' instantiate IE, navigate and get target DOM element
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Visible = True ' for debug only
objIE.Navigate "https://translate.google.com.eg/?hl=ar&tab=wT#en/ar/boy"
Wait objIE
WaitElementById objIE, "gt-lc"
' retrieve HTML content
Set objNode = objIE.Document.GetElementById("gt-lc")
Set objTable = objNode.GetElementsByClassName("gt-baf-table")(0)
strHtmlContent = objTable.outerHTML
strHtmlContent = "<body dir=rtl>" & strHtmlContent & "</body>"
' retrieve CSS content
Set colSSheets = objIE.Document.styleSheets
Set objSSContent = CreateObject("Scripting.Dictionary")
For Each varSSheet In colSSheets
objSSContent(objSSContent.Count) = varSSheet.cssText
Next
objIE.Quit
' Init UserForm, WebBrowser
UserForm1.Show
Set objWB = UserForm1.WebBrowser1
objWB.Navigate "about:blank"
Wait objWB
' put HTML and CSS content into WebBrowser
With objWB.Document
.Write strHtmlContent
Set objHead = .GetElementsByTagName("head")(0)
For Each varCssNumber In objSSContent
objHead.appendChild .createElement("style")
.styleSheets(.styleSheets.Length - 1).cssText = objSSContent(varCssNumber)
Next
End With
End Sub
Sub Wait(objIE)
Do While objIE.ReadyState < 4 Or objIE.Busy
DoEvents
Loop
Do Until objIE.Document.ReadyState = "complete"
DoEvents
Loop
End Sub
Sub WaitElementById(objIE, strId)
Do While IsNull(objIE.Document.GetElementById(strId))
DoEvents
Loop
End Sub
上的结果输出如下:
Chrome中呈现的相同片段: