getElementById doesn't return anything

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
BreakTheWind
Posts: 5
Joined: 19 May 2017, 04:14

getElementById doesn't return anything

04 Jul 2017, 10:51

Hey!

I've been looking at forums for hours but I can't get getElementById to work.

My code:

Code: Select all

^!q::

text := pwb.document.getElementById("DV_NUTRIENT_77").innertext

msgbox %text%
I'm trying to get the protein value from a nutritional website for a certain type of food.

This is the HTML:

Code: Select all

<div class="nf4 left" style="width:31px;">
<span id="DV_NUTRIENT_77">88%</span>
</div>
Why is this not working?
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: getElementById doesn't return anything

04 Jul 2017, 11:29

Hope that's not all of your code?!
That is working fine for me ...

Code: Select all

F1::
    pwb := ComObjCreate( "InternetExplorer.Application" )                       ; Create an IE object 
    pwb.Visible := true                                                         ; Make the IE object visible 
    pwb.Navigate("https://autohotkey.com/board/topic/68594-get-element-help/")  ; Navigate to a webpage 
    while, pwb.busy
      sleep, 10
    MsgBox, % var := pwb.document.getElementById("mqbutton").innertext
    Return
BreakTheWind
Posts: 5
Joined: 19 May 2017, 04:14

Re: getElementById doesn't return anything

04 Jul 2017, 14:20

Thanks, that did work.
I did saw many examples creating the object first, but I saw one that had "pwb" that didn't create an object, so I thought its a variable that creates its own object and does everything automatically, but I was very wrong. It works now, thanks!
I'm not sure how I failed to do it for so much time.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: getElementById doesn't return anything

05 Jul 2017, 07:56

BreakTheWind wrote:Thanks, that did work.
I did saw many examples creating the object first, but I saw one that had "pwb" that didn't create an object, so I thought its a variable that creates its own object and does everything automatically, but I was very wrong. It works now, thanks!
I'm not sure how I failed to do it for so much time.
You should definitely add the below added code, otherwise you'll end up with multiple remaining IE instances (In case you decide to run that thing in "stealth" mode).
[...]
MsgBox, % var := pwb.document.getElementById("mqbutton").innertext "`n" title
Sleep, 500
pwb.quit() ; quit IE instance

Return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 403 guests