cannot detect elements of IE by class

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
SirZoX
Posts: 8
Joined: 29 Jul 2020, 16:24

cannot detect elements of IE by class

15 Aug 2020, 16:29

Hi all.

I'm trying to get all elements from the DOM of IE which have a certain class. I've tried with some answers in this forum but none of them works for me (maybe they all are older answers and the AHK didn't work in the same way today compared with the olders posts.

Well.. my code at this time.

Code: Select all


                        Elements := {}
			Elements := web_browser.document.getElementsByTagName("span")

			nItems := Elements.length()
			MsgBox, items = %nItems% | elements = %Elements%

			Loop %  Elements.length {
			  if (Elements[A_Index-1].getAttribute("class") = "ma-AdTag-label")
			    var := Elements[A_Index-1].value()
			    MsgBox, var %A_Index% = %var%
			}

When this runs, the msgbox returns null values for %nItems% and %Elements%
This code is just an extract of the whole code but the rest is working as expected, just loops inside other loops and some stuff

I've tried to ask at spanish forum too, but any answer for some days so... hope I have better lucky here =)
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: cannot detect elements of IE by class

16 Aug 2020, 12:57

Hola amigo,
SirZoX wrote:
15 Aug 2020, 16:29
I'm trying to get all elements from the DOM of IE which have a certain class.
:arrow: HTML DOM getElementsByClassName() Method

Code: Select all

            Elements := {}
			Elements := web_browser.document.getElementsByTagName("span")

			nItems := Elements.length()
			MsgBox, items = %nItems% | elements = %Elements%
Your first statement creates an ahk object called Elements which it is immediately thereafter overridden by the second statement - so that Elements actually contains a javascript HTMLCollection (array-like object) of elements that match the specified tag name in your document. The third statement queries the Elements length not using the javascript syntax nor the ahk one: hence the script reports nItems as empty. Actually, in javascript, length (just as with value btw) is not a method nor a property: you should not suffix it with parentheses. As for Elements, the MsgBox displays 'nothing' since it is an object; it should display something if you attempt to MsgBox one of its readable properties (e.g. Elements.length incidentally).


Saludos!

[EDIT]| You can use IsObject(ObjectValue) (IsObject) or !!ObjectValue to check if a variable is actually an object (or, respectively, at least not empty) while msgboxing it.


A_AhkUser
my scripts
User avatar
Xeo786
Posts: 759
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

Re: cannot detect elements of IE by class

17 Aug 2020, 05:10

you can simply use .getElementsByClassName("someclass"), and make sure you have IE11, classname thing does not work on previous version of IE see link for support https://www.w3schools.com/jsref/met_document_getelementsbyclassname.asp

If U are one Windows 7 you need to upgrade it to sp1 and then u can upgrade MS.IE to IE11

Code: Select all

Elements := {}
Elements := web_browser.document.getElementsByClassName("ma-AdTag-label")
Loop %  Elements.length 
{
	var := Elements[A_Index-1].value
	MsgBox, var %A_Index% = %var%
}
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: cannot detect elements of IE by class

17 Aug 2020, 16:13

Xeo786 wrote:
17 Aug 2020, 05:10
you can simply use .getElementsByClassName("someclass"), and make sure you have IE11, classname thing does not work on previous version of IE see link for support https://www.w3schools.com/jsref/met_document_getelementsbyclassname.asp

If U are one Windows 7 you need to upgrade it to sp1 and then u can upgrade MS.IE to IE11

Code: Select all

Elements := {}
Elements := web_browser.document.getElementsByClassName("ma-AdTag-label")
Loop %  Elements.length 
{
	var := Elements[A_Index-1].value
	MsgBox, var %A_Index% = %var%
}

Building on this is some example code to make getElementsByClassName work.

Code: Select all

;~ https://autohotkey.com/boards/viewtopic.php?p=136470#p136470

;~ Either change emulation in register or add meta content to HTML

;~ Added newKey AutoHotKey.exe REG_DWORD 11001 to
;~ HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION
RegWrite, REG_DWORD, HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION
, AutoHotkey.exe, 11001

;~ HTML meta content
;~ <meta http-equiv="X-UA-Compatible" content="IE=edge">

html =
(
<div class="example">First div element with class="example".</div>
)


document := ComObjCreate("HTMLfile")
document.open()
document.write(html)
document.close()

MsgBox % document.documentmode

MsgBox % document.getElementsByClassName("example")[0].innerHTML

I went round and round with this problem for months when first learning DOM till I figured out that you have to make sure AHK is using newer version of IE.

There are two approaches to do this. One is edit the Registry to tell Windows to always default to IE 11 when AutoHotkey.exe uses IE. The other is to add the proper meta data at the top of the HTML content.

I tried very had to add the meta data to a DOM document by using DOM commands like createElement, SetAttribute, AppendChild, createTextNode, compatible, userAgent, etc. but never got that approach to work for adding the meta data directly to a DOM object without just getting the HTML, adding the meta text, and then creating a new DOM object from that. Never successfully figured out how to directly add meta data to an existing DOM object. Discovered the Registry thing so now it is a moot point for me on my computer but still a distributing my code problem for others to use.

If any one knows how to add this to the meta data of a DOM object, it would be useful to know. Basically how to change the documentMode to 11.
<meta http-equiv="X-UA-Compatible" content="IE=edge">

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
SirZoX
Posts: 8
Joined: 29 Jul 2020, 16:24

Re: cannot detect elements of IE by class

18 Aug 2020, 16:53

Xeo786 wrote:
17 Aug 2020, 05:10
you can simply use .getElementsByClassName("someclass"), and make sure you have IE11, classname thing does not work on previous version of IE see link for support https://www.w3schools.com/jsref/met_document_getelementsbyclassname.asp

If U are one Windows 7 you need to upgrade it to sp1 and then u can upgrade MS.IE to IE11

Code: Select all

Elements := {}
Elements := web_browser.document.getElementsByClassName("ma-AdTag-label")
Loop %  Elements.length 
{
	var := Elements[A_Index-1].value
	MsgBox, var %A_Index% = %var%
}
Trying this do nothing when executed :(
No msgbox is showed, so I went to confirm if the class really exists on DOM and yes...

Elements.lengt is null in this case :/
SirZoX
Posts: 8
Joined: 29 Jul 2020, 16:24

Re: cannot detect elements of IE by class

18 Aug 2020, 16:55

A_AhkUser wrote:
16 Aug 2020, 12:57
Hola amigo,
SirZoX wrote:
15 Aug 2020, 16:29
I'm trying to get all elements from the DOM of IE which have a certain class.
:arrow: HTML DOM getElementsByClassName() Method

Code: Select all

            Elements := {}
			Elements := web_browser.document.getElementsByTagName("span")

			nItems := Elements.length()
			MsgBox, items = %nItems% | elements = %Elements%
Your first statement creates an ahk object called Elements which it is immediately thereafter overridden by the second statement - so that Elements actually contains a javascript HTMLCollection (array-like object) of elements that match the specified tag name in your document. The third statement queries the Elements length not using the javascript syntax nor the ahk one: hence the script reports nItems as empty. Actually, in javascript, length (just as with value btw) is not a method nor a property: you should not suffix it with parentheses. As for Elements, the MsgBox displays 'nothing' since it is an object; it should display something if you attempt to MsgBox one of its readable properties (e.g. Elements.length incidentally).


Saludos!

[EDIT]| You can use IsObject(ObjectValue) (IsObject) or !!ObjectValue to check if a variable is actually an object (or, respectively, at least not empty) while msgboxing it.


A_AhkUser
Tried with "length" and "value" without parenthesis, but still dont work :(
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: cannot detect elements of IE by class

18 Aug 2020, 17:17

SirZoX wrote:
18 Aug 2020, 16:55
Tried with "length" and "value" without parenthesis, but still dont work :(

Hola,

In this case, it is likely that web_browser variable is empty or is not a instance of Windows Internet Explorer.
Btw, how are you creating/retrieving your instance of the web browser? Is it using ComObjCreate("InternetExplorer.Application"), just as is the case in this code in the spanish forum?
Does:

Code: Select all

MsgBox % IsObject(web_browser)
displays 1 (true)?


A_AhkUser
my scripts
User avatar
Xeo786
Posts: 759
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

Re: cannot detect elements of IE by class

19 Aug 2020, 03:21

SirZoX wrote:
18 Aug 2020, 16:53
Elements.lengt is null in this case :/
sorry coz it lacks brackets :facepalm: it should be >> "Elements.length()"
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory
just me
Posts: 9451
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: cannot detect elements of IE by class

19 Aug 2020, 04:03

Elements is based on the IHTMLElementCollection interface which has a property (not method) length (w/o braces).
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: cannot detect elements of IE by class

19 Aug 2020, 06:12

Hola SirZoX,
SirZoX wrote:
18 Aug 2020, 16:53
I did not notice you give two replies the last time; I've only see the last one, where you quoted me.. (in my defence, it occupies the entire screen so it hid me the previous one)
I should have mentionned that not every HTML elements have a value property. You should try instead either the innerText or the textContent property.
Maybe you could give an extract of the relevant HTML code you are trying to interact with, if possible, to help diagnose the problem, if it persists.

Hope this helps

A_AhkUser
my scripts
SirZoX
Posts: 8
Joined: 29 Jul 2020, 16:24

Re: cannot detect elements of IE by class

27 Aug 2020, 16:58

A_AhkUser wrote:
18 Aug 2020, 17:17
SirZoX wrote:
18 Aug 2020, 16:55
Tried with "length" and "value" without parenthesis, but still dont work :(

Hola,

In this case, it is likely that web_browser variable is empty or is not a instance of Windows Internet Explorer.
Btw, how are you creating/retrieving your instance of the web browser? Is it using ComObjCreate("InternetExplorer.Application"), just as is the case in this code in the spanish forum?
Does:

Code: Select all

MsgBox % IsObject(web_browser)
displays 1 (true)?


A_AhkUser
Nope, returns 0... so web_browser is not an object? just a var?
Edit.. .first I am creating an object called "ie", and on next lines I0ve used "eweb_browser", which is obviously empty because it has never been created before.

When change "web_browser" to "ie" the msgbox returns 1.

Just I can continue the development making some changes: value() per value, length() per length...

Just post my new code when I went stuck again.

Thanks all !!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Lamron750, septrinus and 215 guests