Suhr415
Suhr415

Reputation: 35

VBA web scrape innertext

I have now tried for quite some time to web scrape this innertext: I want the value 0606 copied to an Excel sheet

<TABLE class="group"
<td width="100%" nowrap="" colspan="3">
<input name="pg41_PolicyHolder_FogP_PolicyHolderId_FogP_IdentityQualifier" 
type="HIDDEN" value="CPR">CPR-nr:
<input name="pg41_PolicyHolder_FogP_PolicyHolderId_FogP_IdentityValue" 
type="HIDDEN" value="0606">0606</td>

I have tried through get.attribute,getelementbyclassname, value and innertext, but now I need some fresh eyes on it. Does any of you have a good idea?

Upvotes: 0

Views: 698

Answers (2)

QHarr
QHarr

Reputation: 84465

You can use a CSS selector and avoid the need for a loop.

input[name="pg41_PolicyHolder_FogP_PolicyHolderId_FogP_IdentityValue"]

CSS query

Try it


VBA

.querySelector is accessed via the HTML document set when you have your page (method not shown in your question) but with Internet Explorer for example:

IE.document.querySelector("pg41_PolicyHolder_FogP_PolicyHolderId_FogP_IdentityValue").innerText

Further info:

HTML DOM querySelector() Method

Upvotes: 0

Jordan
Jordan

Reputation: 4514

Something like this should work, however without your code I don't know how you're obtaining your HTMLDocument:

Dim oHTMLDocument As Object
Dim ele As Object

Set oHTMLDocument = ... 'No code provided so I'm unsure how you obtained the HTMLDocument

For Each ele in oHTMLDocument.getElementsByTagName("input")
    If ele.Name = "pg41_PolicyHolder_FogP_PolicyHolderId_FogP_IdentityValue" Then
        Debug.Print ele.innerText
        Exit For
    End If
Next ele

Upvotes: 1

Related Questions