AHK and Selenium Finding and Getting Entire Array by Class and Thanks to Joe Glines

Get help with using AutoHotkey and its commands and hotkeys
EvetsNalon
Posts: 11
Joined: 25 Aug 2018, 17:55

AHK and Selenium Finding and Getting Entire Array by Class and Thanks to Joe Glines

29 Aug 2018, 20:57

Hello, Love the Forum.. I am a bit of a newbie using AHK, but am loving it so far and using it daily.. I am having a problem that is likely a easy one for others, but is evading my current searches and expertise/or lack thereof.. LOL..
Special thanks and shout out has to go to Joe Glines for providing much knowledge and great tutorials that started me on this path and Loving AutoHotkey...

I have successfully connected to a running instance of chrome(in debug mode), and am able to "findElementByClass", for an array with the attribute set at .Attribute("textContent") for a particular class name.. The code I am using seems to only return the very FIRST item in the Array, even when I do not include "item[1]" and add the "s" to the argument.. ie .... "findElementsByClass" .

What I am trying to do is grab the entire array list .Attribute("textContent") for that class.. The number of items in the array changes every few seconds, and I know that I can only get what I get at the instance that the script runs... Or even better, once I am able to get the entire list i would like to iterate over it continually every few seconds or whatever I need number wise.. Grab all the text, then (Final Project goal), eventually be able to search that text for particular strings and the SendKeys and or mouse clicks (Actually both), once script is completed..

Right now though, I am just trying to grab the text from the entire array and display it in a msgbox

Here is the code current code:

Code: Select all

#SingleInstance,Force
LCONTROL::Reload
RCONTROL::

;******************** Connect to Chrome and Find Elements from within the Site ***********************************
driver := ChromeGet()
;~MsgBox, % driver.Title "`n" driver.Url
MsgBox % driver.findElementByClass("_ghbghb") .Attribute("textContent")
return

ChromeGet(IP_Port := "127.0.0.1:9222") {
 driver := ComObjCreate("Selenium.ChromeDriver")
 driver.SetCapability("debuggerAddress", IP_Port)
 driver.Start()
 return driver
}

So anyhow, that is where I am stuck at the moment and ANY assistance would be appreciated.....
Thanks!
EvetsNalon
Posts: 11
Joined: 25 Aug 2018, 17:55

Re: AHK and Selenium Finding and Getting Entire Array by Class and Thanks to Joe Glines (Phase 1 Solved) Phase 2 Need He

01 Sep 2018, 22:00

Hello all, I have been able to figure this one out as to the phase 1 of this project.... Again I am connecting to a running instance of Chrome. (So it stays open all the time even when I reload the script....

Here is finally how that was achieved (Code Below), so that I was able top Find all of the Array outputted to the Debug Window numbered and in order each item in the array comprised of the attribute "innerText" of each element that was the ClassName of ("_ghbghb") .. This WORKS GREAT !!!!

Code: Select all

;~ #NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
;~ #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
;SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.


#SingleInstance,Force
LAlt::Reload ; Just for testing
RCONTROL::   ; Just for testing


;******************** Connect to Chrome and Find Elements from within the Site ***********************************
driver := ChromeGet()

ChromeGet(IP_Port := "127.0.0.1:9222") {
 driver := ComObjCreate("Selenium.ChromeDriver")
 driver.SetCapability("debuggerAddress", IP_Port)
 driver.Start()
 return driver
}
;************* Other Tested Outputs ***********************
;MsgBox, % driver.Title "`n" driver.Url
;MsgBox, % driver.findElementsByClass("__ghbghb") .Attribute("innerText") ;note case sensitive
;MsgBox % driver.FindElementsByClass("__ghbghb").Count ;need to use count
;return


obj:=driver.FindElementsByClass("__ghbghb")
for k in obj {
 ;MsgBox % IsObject(v) a_tab v
	;MsgBox %  a_index a_tab k.Attribute("innerText") "`n"
	x.= a_index k a_tab   k.Attribute("innerText") 
	
}	

DebugWindow(x,1) ; Output to Debug Window after Clearing

Then and Now, I am onto the second phase.. I need to be able to Get or Find these items in the array, and Loop over them until it finds the Text String of "problem" (non-case sensitive preferred), Then send a Key or even a left mouse click then key directly on that Text...


Any Ideas would be greatly appreciated....

Thanks ahead of time... :D
Multiboxer
Posts: 4
Joined: 19 Jul 2017, 04:18

Re: AHK and Selenium Finding and Getting Entire Array by Class and Thanks to Joe Glines

02 Sep 2018, 07:36

I am not familiar with Selenium, I have no way of testing, but would this work?
https://www.w3schools.com/jsref/met_text_select.asp


obj:=driver.FindElementsByClass("__ghbghb")
for k in obj {
;MsgBox % IsObject(v) a_tab v
;MsgBox % a_index a_tab k.Attribute("innerText") "`n"
x.= a_index k a_tab Text := k.Attribute("innerText")
StringLower(Text, Text)
ifequal, Text, "problem"
k.Attribute("innerText").select()
}
EvetsNalon
Posts: 11
Joined: 25 Aug 2018, 17:55

Re: AHK and Selenium Finding and Getting Entire Array by Class and Thanks to Joe Glines

02 Sep 2018, 21:45

Multiboxer wrote:I am not familiar with Selenium, I have no way of testing, but would this work?
https://www.w3schools.com/jsref/met_text_select.asp


obj:=driver.FindElementsByClass("__ghbghb")
for k in obj {
;MsgBox % IsObject(v) a_tab v
;MsgBox % a_index a_tab k.Attribute("innerText") "`n"
x.= a_index k a_tab Text := k.Attribute("innerText")
StringLower(Text, Text)
ifequal, Text, "problem"
k.Attribute("innerText").select()
}


Thanks so much for your input. It is much appreciated...


However, that is not working with this. Error after error. I totally understand you have no way to test that, and again I appreciate the effort at helping... I do think that you are pretty much understanding what is needed for this to work though. I think that it will have to do with something like that.. ie some "For Loop, or other iteration over the innerText content strings", Then Output to test or action...

It should be (I guess what I would believe to be simple coding wise), I just have not been able to wrap my head around it. There should be a way(sorry I am still a Noob at AHK), to Loop over the data which has already been pulled, eg.. "innerText" ... Until AHK finds the Text String "problem", whether it be upper or lower case or combination thereof, and the send a key or mouse click, or for even just testing, send it out to a message box(MsgBox).... Then once tested I could just send either the Keyboard key, and or Mouse click...

Anyhow, Thanks again for Chiming in... As I have been learning, especially in regards to this, I am fairly sure(correct me Anyone, If I am incorrect), that this should be more of a ... AHK scripting code at this point, since the first part(Including the Chrome.AHK, and Selenium has already extracted the data).... Maybe I am totally incorrect about that , but that is what I am thinking at least.. Been at watching tutorials and reading up on so many snippets and concepts today that my mind is about "MUSH".. LOL, at this point..

Thanks again though for your input.. Much appreciated.. I KNOW This cannot be this hard, and It Is Evading me.. Brain Fog I guess....!!!!! Thanks!
Multiboxer
Posts: 4
Joined: 19 Jul 2017, 04:18

Re: AHK and Selenium Finding and Getting Entire Array by Class and Thanks to Joe Glines

03 Sep 2018, 09:04

Hi again.

While searching for something else I noticed this "Finding & Clicking text with Selenium & AutoHotkey" in the search results, and immediately thought of you.

After watching and then re-reading your previous comments, this appears to match my interpretation of your desires.

Have you seen this by Jeo Glines? www(dot)youtube(dot)com/watch?v=BTh5KCcP1Ls

Is this what you are wanting to do, but with different search words?
EvetsNalon
Posts: 11
Joined: 25 Aug 2018, 17:55

Re: AHK and Selenium Finding and Getting Entire Array by Class and Thanks to Joe Glines

04 Sep 2018, 16:49

Multiboxer wrote:Hi again.

While searching for something else I noticed this "Finding & Clicking text with Selenium & AutoHotkey" in the search results, and immediately thought of you.

After watching and then re-reading your previous comments, this appears to match my interpretation of your desires.

Have you seen this by Jeo Glines? www(dot)youtube(dot)com/watch?v=BTh5KCcP1Ls

Is this what you are wanting to do, but with different search words?
Hello again, @Multiboxer.. This seems to be the best idea so far.. Thanks... What is getting me I guess is the syntax or something..

I am using what Joe showed in the video and I can extract that "keyword" into the (debug window), so I know I am getting the keyword that I want, but I cannot seem to get the .click() to work... I have been messing with this for hours and hours and have tried just about all I can think of... Lost at this moment..

I changed some of this code up so that others might also be able to test, by using Joe's website and his exact example so that it might be easier to nail down.. I am pretty sure that this just has to do now with the incorrect form or syntax involved...

Code: Select all

 RCONTROL::


;******************** Connect to Chrome and Find Elements from within the Site ***********************************

driver := ChromeGet()

ChromeGet(IP_Port := "127.0.0.1:9222") {
 driver := ComObjCreate("Selenium.ChromeDriver")
 driver.SetCapability("debuggerAddress", IP_Port)
 driver.Start()
 return driver
}


obj:=driver.FindElementsByClass("continue-reading-link")
for k in obj {
	;MsgBox %  a_index a_tab k.Attribute("innerText") "`n"
	t.= k.Attribute("innerText") 
	
}
;keyword:="Continue reading"
;if(%t% = "keyword").click()
	



DebugWindow(t,1)

Of course different from what Joe was using which was "XPath", I am Using "Class" , as well as then the "innerText" of that Class to then find that string in the innerText....

The script outputs "Continue reading".... Which is exactly what it is supposed to.. Just have to figure out how to get it to click that text string..

It is the if statement that seems to be wrong, and I have tried numerous ways.. No Go!.... Getting frustrated indeed..

Thanks again...

Return to “Ask For Help”

Who is online

Users browsing this forum: Google [Bot], return, songdg, zvit and 49 guests