I am writing a macro in VBA for Excel that examines a page and I need to extract a variable that is set within a script in the HTML. In the macro, I create an IE object thusly:
Set objIE = CreateObject("InternetExplorer.Application")
Pages in the target website all have the following script which defines a variable called digitalData.page.pageName
. Here is a snippet of HTML:
<script>
var digitalData = '';
function init() {
digitalData = {
"user": {
"userLoginState": "guest",
"userCountry": "",
"userEmployeeName": "",
"userBirthday": "",
"userGender": "",
"userState": "",
"userID": "",
"LRUserID": "",
"userEmployeeID": "",
"userDWID": "",
"userSessionId": "BYTEzHFAdLrPoPPOlTPGWvlBjCx54jjEyB8="
},
"page": {
"pageName": "en_us:plp:men:clothing:Casual Shirts",
"pageType": "plp",
"pageGender": "men",
"pageLocale": "us",
"pageRedirected": "no",
"pageJSErrorCount": "3",
"pageLevel1": "men",
"pageLevel2": "men/clothing",
"pageLevel3": "men/clothing/Casual Shirts",
"pageLevel4": "men/clothing/Casual Shirts",
"pageHierarchy": "men/clothing/Casual Shirts"
},
If I open a page from this site in Chrome and inspect it, I can type the variable name in the console and it will return the value but I can't seem to access the variable from IE using VBA like this:
inspectLink(i, 1) = objIE.digitalData.page.pageName
In this case, I would like to find en_us:plp:men:clothing:Casual Shirts
in inspectLink(i, 1)
but instead I get Runtime error '438' Object doesn't support the property or method
.
Dim inspectCat(4) As String
inspectCat(0) = "webcat=men"
inspectCat(1) = "webcat=women"
inspectCat(2) = "webcat=kids"
inspectCat(3) = "webcat=baby"
inspectCat(4) = "webcat=home"
Dim targetSearchCount as Integer
Dim failedSearchCount as Integer
targetSearchCount=0
failedSearchCount=0
REM New Code - DOES NOT WORK cannot access pageName this way
REM if digitalData.page.pageName has en_us: in it, then it's our target
REM if it has failedSearchResult in it, then report to web dev team
REM syntax might be objIE.Document.digitalData.page.pageName
REM inspectLink(i, 1) = objIE.digitalData.page.pageName
REM MsgBox inspectLink(i, 1)
REM if inStr(objIE.digitalData.page.pageName, "en_us:") then targetSearchCount=targetSearchCount+1 endif
REM if inStr(objIE.digitalData.page.pageName, "failed_Search_Result") then failedSearchCount=failedSearchCount+1 endif
REM End New Code
REM Begin Old Code - WORKS BUT "dublicate" MAY NOT BE RELIABLE OVER TIME
REM
Set pageNameDubs = objIE.Document.GetElementsByClassName("page-Name-dublicate")
'MsgBox pageNameDubs(0).Value
For Each pageName In pageNameDubs
' If InStr(pageName.innertext, "en_us:") > 0 Then
inspectLink(i, 1) = pageName.Value
' End If
Next
REM End Old Code