Automating Chrome with AutoHotkey: Setting InnerText, OuterHTML & Values

Web Scraping is Amazing!  Being able to automate Chrome with AutoHotkey has been a long-term goal of mine!  In this video I (quickly) walk through a function I wrote to leverage GeekDude’s Chrome.ahk class.  You can get the class here or read more about it on the AutoHotkey forum.

There’s lots more to do but I wanted to share this to help others get started using the above Class.  🙂

Here is the code I demonstrate in the below video where I demonstrate  setting text in Chrome with AutoHotkey.

#Include B:\Progs\AutoHotkey_L\AHK Work\WebPage\Chrome\Examples\Chrome.ahk  ;GeekDude Class ; https://github.com/G33kDude/Chrome.ahk  https://autohotkey.com/boards/viewtopic.php?t=42890
Tab:=Chm_Create_Instance("C:\Users\joe\AppData\Local\Google\Chrome\User Data\Default") ;Create instance using default Profile
Chm_Navigate(Tab,"https://the-automator.com") ;Navigate to a URL
Chm_Set(Tab,Method:="Class",Attribute:="s",Index:=2,Property:="v",Value:="Just a test")
;~ Chm_Set(Tab,Method:="Name",Attribute:="s",Index:=2,Property:="v",Value:="Just another test")
;~ Chm_Set(Tab,Method:="tag",Attribute:="Input",Index:=1,Property:="v",Value:="Just a test")
;~ Chm_Set(Tab,Method:="ID",Attribute:="cat",Index:=2,Property:="v",Value:="11")


;********************Chrome Set Property******* Propterty=  i=innerText v=Value  o=outerHTML****************************
Chm_Set(Tab,Method="Class",Attribute="hfeed",Index=1,Property="i",Value=""){
  ;***********Class*******************
  if (Format("{:L}",Method)="class"){  ;Case-insensitive check to see if method = Class
    if (Format("{:L}",Property)="o") ;If Property="o" then set OuterHTML
      Tab.Evaluate("document.getElementsByClassName('" Attribute "')[" Index-1 "].outerHTML='" Value "'").Value
    Else if (Format("{:L}",Property)="v") ;If Property="v" then set Value
      Tab.Evaluate("document.getElementsByClassName('" Attribute "')[" Index-1 "].value='" Value "'").Value
    Else if (Format("{:L}",Property)="i") ;If Property="i" then set innerText
      Tab.Evaluate("document.getElementsByClassName('" Attribute "')[" Index-1 "].innerText='" Value "'").Value
  } ;***********Tag*******************
  Else if (Format("{:L}",Method)="tag"){ ;Case-insensitive check to see if method = Tag
    if (Format("{:L}",Property)="o") ;If Property="o" then set OuterHTML
      Tab.Evaluate("document.getElementsByTagName('" Attribute "')[" Index-1 "].outerHTML='" Value "'").Value
    Else if (Format("{:L}",Property)="v") ;If Property="v" then set Value
      Tab.Evaluate("document.getElementsByTagName('" Attribute "')[" Index-1 "].value='" Value "'").Value
    Else if (Format("{:L}",Property)="i") ;If Property="i" then set innerText
      Tab.Evaluate("document.getElementsByTagName('" Attribute "')[" Index-1 "].innerText='" Value "'").Value
  } ;***********Name*******************
  Else if (Format("{:L}",Method)="Name"){ ;Case-insensitive check to see if method = Name
    if (Format("{:L}",Property)="o") ;If Property="o" then set OuterHTML
      Tab.Evaluate("document.getElementsByName('" Attribute "')[" Index-1 "].outerHTML='" Value "'").Value
    Else if (Format("{:L}",Property)="v") ;If Property="v" then set Value
      Tab.Evaluate("document.getElementsByName('" Attribute "')[" Index-1 "].value='" Value "'").Value
    Else if (Format("{:L}",Property)="i") ;If Property="i" then set innerText
      Tab.Evaluate("document.getElementsByName('" Attribute "')[" Index-1 "].innerText='" Value "'").Value
  } ;***********ID*******************
  Else if (Format("{:L}",Method)="id"){ ;Case-insensitive check to see if method = ID
    if (Format("{:L}",Property)="o") ;If Property="o" then set OuterHTML
      Tab.Evaluate("document.getElementById('" Attribute "').outerHTML='" Value "'").Value
    Else if (Format("{:L}",Property)="v") ;If Property="v" then set Value
      Tab.Evaluate("document.getElementById('" Attribute "').value='" Value "'").Value
    Else if (Format("{:L}",Property)="i") ;If Property="i" then set innerText
      Tab.Evaluate("document.getElementById('" Attribute "').innerText='" Value "'").Value
  } Else{ 	MsgBox fix Method- Valid values are: Name, Class, Tag, ID (case of text does not matter)
}
}
;********************Create instance and use a path to profile***********************************
Chm_Create_Instance(Profile_Path=""){
 if !(Profile_Path){
 FileCreateDir, ChromeProfile ;Make sure folder exists
 ChromeInst := new Chrome("ChromeProfile") ;Create a new Chrome Instance
 }Else{
 try ChromeInst := new Chrome(Profile_Path) ;Create for profile lookup prfile by putting this in your url in Chrome chrome://version/
 }
 return Tab := ChromeInst.GetTab() ;Connect to Active tab
}

;********************Navigate to page***********************************
;~ Chm_Navigate(Tab,"https://the-automator.com")
Chm_Navigate(Tab,URL){
 Tab.Call("Page.navigate", {"url": URL}) ;Navigate to URL
 Tab.WaitForLoad() ;Wait for page to finish loading
}

 

Here’s the video walking through how to use the function with the above

Chrome.ahk Class to setting text in Chrome with AutoHotkey

 

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.