Using IE.Document.GetElementsByClassname

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
bigbo718
Posts: 1
Joined: 03 Mar 2017, 15:29

Using IE.Document.GetElementsByClassname

03 Mar 2017, 15:38

Hey Guys,

i'm trying to pull information from a web browser using the GetElementsByClassname method and continue to get the following error:

---------------------------
Test.ahk
---------------------------
Error: 0x80020006 - Unknown name.

Specifically: getElementsbyClassName

---> 015: Client_Name := Pwb1.document.getElementsbyClassName("PSEDITBOX")


was wondering if anyone gets this error and am i doing something incorrectly.
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Using IE.Document.GetElementsByClassname

04 Mar 2017, 02:20

You really need to post your code and more information. Specifically how Pwb1.document was created.

All I can tell you from that error message is that Pwb1.document does not possess the property getElementsbyClassName. The HTML could be telling Windows to interpret the HTML code like an older version of IE which does not support getElementsbyClassName or you could only have an old version of IE to interpret the HTML code.

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
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Using IE.Document.GetElementsByClassname

09 Mar 2017, 00:33

Here is some example code which might help you achieve what you want.
When it tried to refer to an element that didn't exist, there was no error message.

Code: Select all

q::
;link for WBGet:
;Basic Webpage Controls with JavaScript / COM - Tutorial - Tutorials - AutoHotkey Community
;https://autohotkey.com/board/topic/47052-basic-webpage-controls-with-javascript-com-tutorial/

;open this url in Internet Explorer:
;AutoHotkey Community - Index page
;https://autohotkey.com/boards/

WinGet, hWnd, ID, ahk_class IEFrame
oWB := WBGet("ahk_id " hWnd)
MsgBox % oWB.document.getElementsbyClassName("nav-link").length
MsgBox % oWB.document.getElementsbyClassName("nav-link").item[0].innerText
MsgBox % oWB.document.getElementsbyClassName("PSEDITBOX").length
MsgBox % oWB.document.getElementsbyClassName("PSEDITBOX").item[0]
MsgBox % oWB.document.getElementsbyClassName("PSEDITBOX")
oWB := ""
Return
It would be interesting if you post your code that isn't working, or tell us what the problem was if you figure it out.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Using IE.Document.GetElementsByClassname

09 Mar 2017, 02:12

jeeswg wrote:When it tried to refer to an element that didn't exist, there was no error message.
The problem is not that the element does not exist but that the document does not support getElementsByClassName which is easy to reproduce.

Code: Select all

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

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

MsgBox % document.getElementsByClassName("example")[0].innerHTML
That will give the same error message.

Now it will work.

Code: Select all

html =
(
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<div class="example">First div element with class="example".</div>
)

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

MsgBox % document.getElementsByClassName("example")[0].innerHTML
All HTML documents do not support getElementsByClassName.

HTML using protocols before IE9 does not support getElementsByClassName.

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
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Using IE.Document.GetElementsByClassname

09 Mar 2017, 03:12

Yes, I came across exactly this problem within the last few days. I guessed that that might be the problem, that HTMLfile objects couldn't support certain methods. How old are these objects, I couldn't find any good official links for HTMLfile objects when I searched, I can't even be sure of the capitalisation right now.

[EDIT:]
That reminds me, for an object, or for a GUI class like 'Edit' or 'SysListView32', is it possible to get information like a version number.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: Using IE.Document.GetElementsByClassname

09 Mar 2017, 19:30

Are these DOM methods case sensitive like in JS? Not at a computer to test but was curious.
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Using IE.Document.GetElementsByClassname

09 Mar 2017, 20:00

kczx3 wrote:Are these DOM methods case sensitive like in JS? Not at a computer to test but was curious.
The method and property names like getElementsByClassName are not case sensitive. getelementsbyclassname will work fine but the element names like "example" above are case sensitive.

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
TravisQ
Posts: 27
Joined: 17 May 2015, 23:51

Re: Using IE.Document.GetElementsByClassname

09 Mar 2017, 23:49

Added newKey AutoHotKey.exe REG_DWORD 11001 to

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION

and both scripts by FG are recognized
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Using IE.Document.GetElementsByClassname

10 Mar 2017, 14:06

TravisQ wrote:Added newKey AutoHotKey.exe REG_DWORD 11001 to

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION

and both scripts by FG are recognized
Great information to know. Much appreciated.

I tried it and it works for me.

I have tried several times to get a solution by modifying the document object. The HTML code tells the document object what flavor of IE to use for interpreting it. Which means adding <meta http-equiv="X-UA-Compatible" content="IE=edge"> to the HTML code causes the document object to use a newer version of IE to interpret. But if you have no control over changing the HTML code before it is put into the document object it is much harder to tell the document object how to interpret the HTML code.

You can look at the document.compatible.0.userAgent and see the userAgent which has to do with how the HTML code is interpreted but I have been unable to add a userAgent.

I also tried creating a TextNode with http-equiv="X-UA-Compatible" content=""IE=edge" and adding the node to the HTML code but I could not get that to work either.

The RegEdit solution will work for my computer but does not solve the problem for distributing script to others but I may just have to have my script modify their Registry which I don't like messing with other computers' Registry but it might be the best way to go.

Thanks for the insight.

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
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Using IE.Document.GetElementsByClassname

18 Jun 2018, 16:08

You can try IE=edge as was mentioned, but also IE=9:
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="X-UA-Compatible" content="IE=9">
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Using IE.Document.GetElementsByClassname

18 Jun 2018, 18:56

jeeswg wrote:You can try IE=edge as was mentioned, but also IE=9:
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="X-UA-Compatible" content="IE=9">
All that is great but the point is, I have found no way to set the IE version after the document object is created.

Code: Select all

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

document := ComObjCreate("HTMLfile")
document.open()
document.write(html)
document.close()
; without changing anything above, how to change the IE version of the document is the problem
;
MsgBox % document.getElementsByClassName("example")[0].innerHTML
From what I can tell, you either got to change the registry or the HTML before the HTML is added to the document. If the document is being created from a website through a browser object then you have no access to the HTML before it is added to the document and from what I can tell, once a document is fully created, its version is unchangeable.

I have found no way to interject the equivalent of "<meta http-equiv="X-UA-Compatible" content="IE=edge">" into the document after it has been fully created with HTML and have it take effect.

You could maybe pull all the HTML code from a document. Modify the HTML code as a string and then create a new document based on the modified HTML code.

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

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: drani, Freddie, gongnl, Google [Bot], haomingchen1998, mmflume, ShatterCoder and 93 guests