AHK for Chrome/Edge Elements

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
monicakes123
Posts: 6
Joined: 17 Jun 2022, 10:13

AHK for Chrome/Edge Elements

Post by monicakes123 » 01 Jul 2022, 10:16

Hi, I am trying to open a webpage on chrome, login, then move to the reporting page and run and download a report automatically on either a chrome or edge browser. I only got as far as logging in. (I was able to get a little farther by searching the page for specific words I was looking for with control f and then clicking the searched item but I don't think that's an efficient way of going about this). I'm trying to see if there is a way to go to information on the page by it class name, tag name, id?

Code: Select all

Run chrome.exe "https://accudata.myisolved.com/UserLogin.aspx?ReturnUrl"
Send,%user_name%
Send {Enter}
SendInput,%pass_word%
Send {Enter}
Thank you!

ShatterCoder
Posts: 75
Joined: 06 Oct 2016, 15:57

Re: AHK for Chrome/Edge Elements

Post by ShatterCoder » 01 Jul 2022, 15:30

Yes there is absolutely a better way to do this. download chrome.ahk here: https://github.com/G33kDude/Chrome.ahk/releases and extract it to a subfolder in your scripts directory named "lib"

then you can do this:

Code: Select all

#include <chrome.ahk>

IfNotExist, C:\Temp\ChromeProfile
	FileCreateDir, C:\Temp\ChromeProfile

global PageInst := "", ChromeInst := new Chrome("C:\AHK\ChromeProfile") ;decalare SuperGlobals (available in ALL scopes)

;************************************************************************* Navigate to the destination *************************************************************************
if !(PageInst := ChromeInst.GetPage())
{
	MsgBox, Could not retrieve page!
	ChromeInst.Kill()
}
else
{
	; --- Navigate to the desired URL ---
	PageInst.WaitForLoad()
	PageInst.Call("Page.navigate", {"url": "https://accudata.myisolved.com/UserLogin.aspx?ReturnUrl"})
	PageInst.WaitForLoad()
	
	;use this if you want to test/confirm what URL the page is currently on
	Test = document.URL 
	Result := PageInst.Evaluate(Test)
	URL := Result.value ;URL is now the current address

	SetValue("id_username", "User_name") ;this function targets a field by its ID tag 
	SetValue("id_password", "Pass_word")
	TestClause = document.getElementsByClassName("grp-button grp-default").item(0).click() ;Here you will need to do some research and figure out how to target the button used to login this is an example from a site I use
	PageInst.Evaluate(TestClause)
	
;************************************************************************* AutoFill *************************************************************************
	PageInst.WaitForLoad()
	SetValue("id_part_number", PartNumber) ;example of setting a value using a var
	SetValue("id_description", "elbow fitting") ;example of setting a value using a string
	
return

SetValue(IDin, ValueIn) ;function used as a wrapper for setting values within the form
{
	;function takes in the name of an element ID and a value to set it to then returns the result
	;after sending to chrome.ahk's Evaluate method.
	TestClause = document.getElementById("%IDin%").value = '%ValueIn%';
	Result := PageInst.Evaluate(TestClause)
	return Result
}
You may need to learn a bit of js and query selectors to figure out how to get what you want, but using chrome.ahk you can make filling out forms a snap.

Hope this helps

Post Reply

Return to “Ask for Help (v1)”