Help Getting innerText from Web Element

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
hobbycoder1984
Posts: 15
Joined: 28 Jan 2022, 11:36

Help Getting innerText from Web Element

18 Feb 2024, 11:21

Hello all,

I'm trying to get the country name web element from this website: https://www.scrapethissite.com/pages/simple/

It seems straightforward. I think there must be a piece I'm missing or a small error that I can't see. When I run the command in the console, I get the correct element returned.

Code: Select all

^1::
GetCountryInfo := "https://www.scrapethissite.com/pages/simple/"
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("GET", GetCountryInfo)
whr.Send()
Sleep 100
CountryInfo := whr.ResponseText

document := ComObjCreate("HTMLFile")
document.write(CountryInfo)

msgbox, % document.getElementsByClassName("country-name")[0].innerText
Return
From the Console, this returns the right element: document.getElementsByClassName("country-name")[0].innerText

What am I doing wrong?

Thanks!
User avatar
lmstearn
Posts: 698
Joined: 11 Aug 2016, 02:32
Contact:

Re: Help Getting innerText from Web Element

19 Feb 2024, 00:12

There are issues with the Write() method as explained in that link:

Code: Select all

GetCountryInfo := "https://www.scrapethissite.com/pages/simple/"
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("GET", GetCountryInfo)
whr.Send()
WebRequest.WaitForResponse()
GetCountryInfo := whr.ResponseText

document := ComObjCreate("HTMLFile")
document.write(GetCountryInfo)
	if document.Url
	msgbox, % document.getElementsByClassName("country-name")[0].innerText
	else
	msgbox No URL for Document!
Return
It might work if the file is downloaded with the following string inserted at the beginning of the file as described in the answer here:

Code: Select all

<meta http-equiv="X-UA-Compatible" content="IE=edge">`n
Edit: As you probably know, the site employs Python and the BeautifulSoup library for scraping, a perusal of that may reveal more.
HTH. :)
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
garry
Posts: 3795
Joined: 22 Dec 2013, 12:50

Re: Help Getting innerText from Web Element

20 Feb 2024, 03:45

@lmstearn thank you , was playing to get a formatted text

Code: Select all

;- Help Getting innerText from Web Element 
;- https://www.autohotkey.com/boards/viewtopic.php?f=76&t=126230
;- FONT for notepad example Lucida Console
;-
F1:=a_desktop . "\test22.txt"
GetCountryInfo := "https://www.scrapethissite.com/pages/simple/"
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("GET", GetCountryInfo)
whr.Send()
WebRequest.WaitForResponse()
GetCountryInfo := whr.ResponseText
document := ComObjCreate("HTMLFile")
document.write(GetCountryInfo)
I:=0
if document.Url
	 {
	 e4x:="Country-Name;Capital;Population;Area`r`n--------;--------;-------;-----`r`n"
	 ;msgbox, % document.getElementsByClassName("country-name")[1].innerText  ;- 0=Andorra , 1=United arab Emirates
	 ;loop,250
	 loop,10
	   {
	   u:= % document.getElementsByClassName("country-name")[i].innerText
	   v:= % document.getElementsByClassName("country-capital")[i].innerText
	   w:= % document.getElementsByClassName("country-population")[i].innerText
	   x:= % document.getElementsByClassName("country-area")[i].innerText
	   e4x .= u . "`;" . v . "`;" . w . "`;" . x . "`r`n"
	   i++
	   }
	 } 
	else
	msgbox No URL for Document!
if e4x<>
 {
 filedelete,%f1% 
 NewCol := ColWidth(e4x)
 fileappend,%newcol%,%f1%,utf-8
 try,run,%f1%	
 }
Exitapp
;-----------------------------------------------------
;---- function linepadding formatx -------------------
;- https://www.autohotkey.com/boards/viewtopic.php?style=7&t=6815
ColWidth(e4x)
{
   ColWidth =25,25,15,14
   dlm:=";"
   StringSplit,ColWidth_, ColWidth,`,
   Loop, %ColWidth_0%
         {
         if (a_index=3 or a_index=4)   ;- columns 3+4 to be right aligned
            ResColumn .=  "{:" ColWidth_%A_Index% "}" ((ColWidth_0 = A_Index) ? "" : dlm)
         else
            ResColumn .=  "{:-" ColWidth_%A_Index% "}" ((ColWidth_0 = A_Index) ? "" : dlm)
         }
    Loop Parse, e4x, `n, `r
       {
       If StrLen(a_loopfield) < 1
            Continue
        a := StrSplit(a_loopfield,dlm)
        NewLine  .= Format( ResColumn . "|`r`n",a*)
        ;NewLine .= Format("{:-20}|{:-20}|{:-20}`n",a*)
       }
    Return NewLine
}
;======================================================
example output

Code: Select all

Country-Name             ;Capital                  ;     Population;          Area|
--------                 ;--------                 ;        -------;         -----|
 Andorra                 ;Andorra la Vella         ;          84000;         468.0|
 United Arab Emirates    ;Abu Dhabi                ;        4975593;       82880.0|
 Afghanistan             ;Kabul                    ;       29121286;      647500.0|
 Antigua and Barbuda     ;St. John's               ;          86754;         443.0|
 Anguilla                ;The Valley               ;          13254;         102.0|
 Albania                 ;Tirana                   ;        2986952;       28748.0|
 Armenia                 ;Yerevan                  ;        2968000;       29800.0|
 Angola                  ;Luanda                   ;       13068161;     1246700.0|
 Antarctica              ;None                     ;              0;         1.4E7|
 Argentina               ;Buenos Aires             ;       41343201;     2766890.0|

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, macromint, peter_ahk and 330 guests