Grab adjacent div or span class for Chrome.ahk Class

Viewed 564

I'm trying to create a script to grab the textcontent for the +1 or next SPAN/DIV element. I'm using AHK and the Chrome.Ahk class to help web scrape on chrome. I figured out how to do exactly what I want in IE DOM but having trouble converting it to chrome.

Here is my script that works in IE DOM that is grabbing the next element.

All:=WB.Document.GetElementsByTagName("Span") ;Get Tagname and Array value
while(aa:=All.Item[A_Index-1]){
    if(aa.InnerText="Modem MAC:"){ ;*[Grab Info from Gateway]
        msgbox % aa.ParentNode.GetElementsByTagName("Span").Item[1].InnerText
    }
}

Here is my script for Chrome that grabs the first element; or, the element I search for:

PageInst.Evaluate("document.getElementsByTagName('Span').textContent='Modem MAC:'"))

I'm puzzled how to translate my IE DOM code to the chrome class which needs to be in JS syntax as I read. How do I grab literally the next SPAN class or as chrome shows it a div.

EDIT:

I was able to finally figure out how to loop through each element. But still having trouble getting my desired result. But here is the loop I came up with. Anyone have any further thoughts?

while(PageInst.Evaluate("document.getElementsByTagName('div')['" A_Index "']")){
    msgbox % PageInst.Evaluate("document.getElementsByTagName('div')['" A_Index "'].textContent").Value
}

Edit #2:After a week of tackling this I've gotten really damn close but I think my syntax is slightly wrong. The code below works on IE

WB := WBGet()
while(WB.Document.GetElementsByTagName("Span").Item[A_Index-1]){
    if(WB.Document.GetElementsByTagName("Span").Item[A_Index-1].InnerText="Modem MAC:"){ ;*[Grab Info from Gateway]
        msgbox % WB.Document.GetElementsByTagName("Span").Item[A_Index-1].nextSibling.GetElementsByTagName("Div")[0].innerText
    }
}

This is my attempt at changing the syntax and could use another pair of eyes as what I'm not correctly translating?

Tab:=PageInst
while(Tab.Evaluate("document.getElementsByTagName('Span')['" A_Index "']")){
    if(Tab.Evaluate("document.getElementsByTagName('Span')['" A_Index "'].innerText='Modem MAC:'")){
        msgbox % Tab.Evaluate("document.getElementsByTagName('Span')['" A_Index "'].nextSibling.getElementsByTagName('Div')[0].innerText").Value
    }   
}
1 Answers

Thank you Geekdude for pointing me in the right direction, using JS to search for it and convert back to ahk.

MsgBox, % PageInst.Evaluate("[].filter.call(document.querySelectorAll('span'), e=>e.innerText=='Modem MAC:')[0].nextSibling.querySelector('div').innerText").value
Related