document.getElementsbyClassName

Get help with using AutoHotkey and its commands and hotkeys
Jrm
Posts: 6
Joined: 13 Jun 2018, 14:58

document.getElementsbyClassName

13 Jun 2018, 15:09

Hello everyone,

I just strated to learn AHK two days ago and i'm currently trying to use the function getElementsbyClassName.

In detail :

- inside this class, i need to save this number "30452" inside my variable Number

<span class="_dtd _30b5"> 30452</span>

For that, i wrote this line but this is not working. someone can help ?

Code: Select all

pwb.Navigate(group) ;Navigate to URL define above in my script

Number := pwb.document.getElementsbyClassName("_dtd _30b5")[0].innerText

MsgBox, My number is :  "%Number%"
please place your code in [code][/code] tags!


Thank you a lot,
FanaticGuru
Posts: 1220
Joined: 30 Sep 2013, 22:25

Re: document.getElementsbyClassName

13 Jun 2018, 16:44

Jrm wrote:Hello everyone,

I just strated to learn AHK two days ago and i'm currently trying to use the function getElementsbyClassName.

In detail :

- inside this class, i need to save this number "30452" inside my variable Number

<span class="_dtd _30b5"> 30452</span>

For that, i wrote this line but this is not working. someone can help ?

pwb.Navigate(group) ;Navigate to URL define above in my script

Number := pwb.document.getElementsbyClassName("_dtd _30b5")[0].innerText

MsgBox, My number is : "%Number%"


Thank you a lot,
The below thread might help:
https://autohotkey.com/boards/viewtopic ... 70#p136470

HTML comes in different flavors and not all of them supports GetElementsByClassName. The HTML can state what flavor it is with something like <meta http-equiv="X-UA-Compatible" content="IE=edge"> or you can tell Windows through a EditReg to use a certain flavor by default.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
User avatar
TLM
Posts: 1339
Joined: 01 Oct 2013, 07:52

Re: document.getElementsbyClassName

13 Jun 2018, 17:07

Also after the page is loaded try

Code: Select all

spanObj := pwb.document.getElementsByTagName( "span" )

While ( a_index-1 < spanObj.length )
{
	if ( spanObj[ a_index-1 ].className = "_dtd _30b5" )
	{
		spanValue := Trim( spanObj[ a_index-1 ].innerText, " " )
	}
}

msgbox % spanValue
the className property is your friend ;)
FanaticGuru
Posts: 1220
Joined: 30 Sep 2013, 22:25

Re: document.getElementsbyClassName

13 Jun 2018, 18:30

TLM wrote:Also after the page is loaded try

Code: Select all

spanObj := pwb.document.getElementsByTagName( "span" )

While ( a_index-1 < spanObj.length )
{
	if ( spanObj[ a_index-1 ].className = "_dtd _30b5" )
	{
		spanValue := Trim( spanObj[ a_index-1 ].innerText, " " )
	}
}

msgbox % spanValue
the className property is your friend ;)
Yea, before I figured out the RegEdit to make getElementsByClassName to work, I had used a function to loop though getElementsByTagName and look for the right ClassName. ByTagName is older and supported by even the lowest default DocumentMode.

If you are ok with adding a Register entry, you can add this line to the top of your script.

Code: Select all

RegWrite, REG_DWORD, HKEY_CURRENT_USER\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION
, AutoHotkey.exe, 11001
Assuming you actually have a version of IE on your computer that supports ByClassName.

It just tells Windows to always use the highest mode available up to Edge mode when AutoHotkey uses Internet Explorer.

Technically, it only needs to be run once but doesn't really do any harm to reset it.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
User avatar
TLM
Posts: 1339
Joined: 01 Oct 2013, 07:52

Re: document.getElementsbyClassName

13 Jun 2018, 19:24

FanaticGuru wrote:Assuming you actually have a version of IE on your computer that supports ByClassName.
exactly :lol:
Jrm
Posts: 6
Joined: 13 Jun 2018, 14:58

Re: document.getElementsbyClassName

14 Jun 2018, 11:07

Thank you both for the help ! both answers are working with no error code but i do not have the number display inside the MsgBox. Actually the MsgBox is empty with

Code: Select all



spanObj := pwb.document.getElementsByTagName( "span" )

While ( a_index-1 < spanObj.length )
{
	if ( spanObj[ a_index-1 ].className = "_dtd _30b5" )
	{
		spanValue := Trim( spanObj[ a_index-1 ].innerText, " " )
	}
}

msgbox % spanValue


FanaticGuru
Posts: 1220
Joined: 30 Sep 2013, 22:25

Re: document.getElementsbyClassName

14 Jun 2018, 13:34

Jrm wrote:Thank you both for the help ! both answers are working with no error code but i do not have the number display inside the MsgBox. Actually the MsgBox is empty with

Code: Select all



spanObj := pwb.document.getElementsByTagName( "span" )

While ( a_index-1 < spanObj.length )
{
	if ( spanObj[ a_index-1 ].className = "_dtd _30b5" )
	{
		spanValue := Trim( spanObj[ a_index-1 ].innerText, " " )
	}
}

msgbox % spanValue


It is hard to say exactly what your problem but below is code that might better help you to understand some aspects of DOM.

Code: Select all

html =
(
<span class="example"> 12345</span>
<div class="example">First div element with class="example".</div>
<span class="_dtd _30b5"> 30452</span>
)

document := ComObjCreate("HTMLfile")
document.open()
document.write(html)
document.close()
; above is all just to create a document for testing

Elements := {}
Elements := document.getElementsByTagName("span") ; get a collection of all "span" elements
Loop %  Elements.length ; loop through all the elements of the collection and report information
	MsgBox % "Index = " A_Index-1 "`nClass = " Elements[A_Index-1].getAttribute("class") "`nValue = " Elements[A_Index-1].innerText
Instead of reporting information, there could be an "if" statement to check for various properties.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
Jrm
Posts: 6
Joined: 13 Jun 2018, 14:58

Re: document.getElementsbyClassName

15 Jun 2018, 01:34

FanaticGuru wrote:
Jrm wrote:Thank you both for the help ! both answers are working with no error code but i do not have the number display inside the MsgBox. Actually the MsgBox is empty with

Code: Select all



spanObj := pwb.document.getElementsByTagName( "span" )

While ( a_index-1 < spanObj.length )
{
	if ( spanObj[ a_index-1 ].className = "_dtd _30b5" )
	{
		spanValue := Trim( spanObj[ a_index-1 ].innerText, " " )
	}
}

msgbox % spanValue


It is hard to say exactly what your problem but below is code that might better help you to understand some aspects of DOM.

Code: Select all

html =
(
<span class="example"> 12345</span>
<div class="example">First div element with class="example".</div>
<span class="_dtd _30b5"> 30452</span>
)

document := ComObjCreate("HTMLfile")
document.open()
document.write(html)
document.close()
; above is all just to create a document for testing

Elements := {}
Elements := document.getElementsByTagName("span") ; get a collection of all "span" elements
Loop %  Elements.length ; loop through all the elements of the collection and report information
	MsgBox % "Index = " A_Index-1 "`nClass = " Elements[A_Index-1].getAttribute("class") "`nValue = " Elements[A_Index-1].innerText
Instead of reporting information, there could be an "if" statement to check for various properties.

FG




Thank for the answer !

I tried all of this but no MsgBox appears... Not easy to understand AHK :shock:

WIll it work if we have an HTML more like this with the <span> inside a <div> ?

Code: Select all

html =
(
<div class="EXAMPLE">
<a class="_test" href="XXXXXXXXX_">Numbers</a>
<span class="_dtd _30b5"> 30452</span>
</div>
)
or even if we have :

Code: Select all

html =
(
<div class="LVL0">
<div class="LVL1">
<div class="LVL2">
<a class="_test" href="XXXXXXXXX_">Numbers</a>
<span class="_dtd _30b5"> 30452</span>
</div>
</div>
</div>
)
FanaticGuru
Posts: 1220
Joined: 30 Sep 2013, 22:25

Re: document.getElementsbyClassName

15 Jun 2018, 16:13

Code: Select all

html =
(
<span class="example"> 12345</span>
<div class="example">First div element with class="example".</div>
<span class="_dtd _30b5"> 30452</span>
)

document := ComObjCreate("HTMLfile")
document.open()
document.write(html)
document.close()
; above is all just to create a document for testing

Elements := {}
Elements := document.getElementsByTagName("span") ; get a collection of all "span" elements
Loop %  Elements.length ; loop through all the elements of the collection and report information
	MsgBox % "Index = " A_Index-1 "`nClass = " Elements[A_Index-1].getAttribute("class") "`nValue = " Elements[A_Index-1].innerText
I don't know what to tell you. This code works fine for me on multiple different computers.

You might try putting this line in MsgBox % document.documentmode to verify the version of IE being used.

And this is all under the basic assumptions that you have Internet Explorer and AutoHotkey installed properly on your computer. That both are up-to-date and not using an very old version of either.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
Jrm
Posts: 6
Joined: 13 Jun 2018, 14:58

Re: document.getElementsbyClassName

16 Jun 2018, 02:22

Thank you all for the help ! Actually your proposals are working but before pull out the information i had to add a sleeping time after the "navigate" action.

Code: Select all


pwb.Navigate(group) ;Navigate to URL

while pwb.busy or pwb.ReadyState != 4 ;Wait for page to load
	Sleep, 100
	
THANK YOU :D :D

Return to “Ask For Help”

Who is online

Users browsing this forum: Near, swagfag and 71 guests