• Intro to AutoHotkey HotStrings with AutoHotkey Intermediate AutoHotkey GUIs are Easy with AutoHotkey Intro to DOS & AutoHotkey

Automating Chrome with AutoHotkey: Getting InnerText, OuterHTML & Values

AutoHotkey Merchandise-White Stress ballBeing able to Automate Chrome has been a long-time goal of mine.  With GeekDude’s Chrome.ahk Class you now can do this.  Read more about his class on the AHK forum here.

If you didn’t see it, also check out my other tutorial on Setting values with Chrome and AutoHotkey.

 

Here is the function I demonstrate in the below video on Automating Chrome with AutoHotkey: Getting InnerText, OuterHTML & Values
Tab := Chrome.GetTab() ;Connect to Active tab

;********************Methd: Class, Tag, Name, ID **** ***********************************
data:=Chm_Get(Tab,Method:=”Name”,Attribute:=”s”,Index:=2)
;~ MsgBox % data.InnerText
DebugWindow(ExploreObj(data),1,1,200,0)

;********************Method: Class, Tag, Name, ID **** ***********************************
;********************Get Property***********************************
Chm_Get(Tab,Method=”Class”,Attribute=”hfeed”,Index=1){
Obj:={}
;***********Class*******************
if (Format(“{:L}”,Method)=”class”){ ;Case-insensitive check to see if method = Class
Obj.OuterHTML:= Tab.Evaluate(“document.getElementsByClassName(‘” Attribute “‘)[” Index-1 “].outerHTML”).Value
Obj.Value:= Tab.Evaluate(“document.getElementsByClassName(‘” Attribute “‘)[” Index-1 “].value”).Value
Obj.InnerText:= Tab.Evaluate(“document.getElementsByClassName(‘” Attribute “‘)[” Index-1 “].innerText”).Value
Obj.TextContent:= Tab.Evaluate(“document.getElementsByClassName(‘” Attribute “‘)[” Index-1 “].textContent”).Value
} ;***********Tag*******************
Else if (Format(“{:L}”,Method)=”tag”){ ;Case-insensitive check to see if method = Tag
Obj.OuterHTML:= Tab.Evaluate(“document.getElementsByTagName(‘” Attribute “‘)[” Index-1 “].outerHTML”).Value
Obj.Value:= Tab.Evaluate(“document.getElementsByTagName(‘” Attribute “‘)[” Index-1 “].value”).Value
Obj.InnerText:= Tab.Evaluate(“document.getElementsByTagName(‘” Attribute “‘)[” Index-1 “].innerText”).Value
Obj.TextContent:= Tab.Evaluate(“document.getElementsByTagName(‘” Attribute “‘)[” Index-1 “].textContent”).Value
} ;************Name******************
Else if (Format(“{:L}”,Method)=”name”){ ;Case-insensitive check to see if method = Name
Obj.OuterHTML:= Tab.Evaluate(“document.getElementsByName(‘” Attribute “‘)[” Index-1 “].outerHTML”).Value
Obj.Value:= Tab.Evaluate(“document.getElementsByName(‘” Attribute “‘)[” Index-1 “].value”).Value
Obj.InnerText:= Tab.Evaluate(“document.getElementsByName(‘” Attribute “‘)[” Index-1 “].innerText”).Value
Obj.TextContent:= Tab.Evaluate(“document.getElementsByName(‘” Attribute “‘)[” Index-1 “].textContent”).Value
} ;***********ID*******************
Else if (Format(“{:L}”,Method)=”id”){ ;Case-insensitive check to see if method = ID
Obj.OuterHTML:= Tab.Evaluate(“document.getElementById(‘” Attribute “‘).outerHTML”).Value
Obj.Value:= Tab.Evaluate(“document.getElementById(‘” Attribute “‘).value”).Value
Obj.InnerText:= Tab.Evaluate(“document.getElementById(‘” Attribute “‘).innerText”).Value
Obj.TextContent:= Tab.Evaluate(“document.getElementById(‘” Attribute “‘).textContent”).Value
} Else{ MsgBox fix Attribute- Valid values are: Name, Class, Tag, ID
}
return obj
}

 

 

 

Video Tutorial Automating Chrome with AutoHotkey: Getting InnerText, OuterHTML & Values

Automating Chrome with AutoHotkey: Setting InnerText, OuterHTML & Values

Black Bottle 2Web 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,"http://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,"http://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

 

 

Update to Web Scraping syntax writer for AutoHotkey

I made a few updates to my Web Scraping syntax writer.  This new version adds in web scrapingfunctionality to Get/Set attributes of an element as well easily find all tables on a given page, then extract the text and dump it into a dynamic Listview.

Both can be pretty helpful when scraping data from the web.  For an extensive review of the tool, checkout the videos on my main Web Scraping page.

Demonstration of updated Web Scraping syntax writer

 

AutoHotkey Webinar- Intro to Web Scraping

In this AutoHotkey Webinar we cover an Intro to Web Scraping.

Make sure you get our Web Scraping Syntax writer!

Intro to Web Scraping

Video Hour 1: High-level:

  • What is an API call
  • Web Scraping vs. WebServices / API
  • Document Object Model (DOM)
  • Structure of a web page
  • Frequently used Methods & properties

Video Hour 2: Coding and Q&A

  • Building code
  • Using AutoHotkey syntax writer
  • IE verse Edge
  • Selenium for Chrome, Firefox, IE , etc.

Script Highlight: Long-pressing Hotkey to Exit a script

The below script is more of an example of how you can have more than one hotkey for a given key by tying it to how long you press the key.  In the below script, if you hold down Escape longer than 1/2 a second it will close the active program.

PowerPoint Deck presented in Intro to Web Scraping

Q) What is an API? (Application Programming Interface?)

A) Basically it is how devices (computers, tablets, phones, servers, etc) talk to each other.

What is an API

Q) What are examples of common API calls

A) Below are a list of some examples (but the list is limitless)

  • Web Browser pulling up products on Amazon.com
  • Application / Software querying products for sale on Amazon.com
  • App on your phone getting latest Weather
  • Database pulling updated sales report
  • Using your Tablet to Select movies to watch on Netflix
  • DropBox application syncing files between your computer & cloud
  • Using Web browser to access files in DropBox

Q) What are the Main Differences between Webservice / API call & Web Browser

webservice verse browser

Q) What is the HTML DOM: Document Object Model

A) Think of the DOM as the “road map” for a webpage.  Depending on what you are trying to get/set, you’ll rely on the DOM to get the exact element you’re looking for.   Below is an image which stresses the more-frequently-used parts.

HTML DOM

Q) How does a Family Tree similar to the DOM?

A) Both are “hierarchical” and, depending on what elements are populated, you can reliably “talk” to the correct node.  The first tree below shows how some nodes have all elements populated while some are missing some (like Name and ID)

family tree 1

Q) What happens if a new Node is inserted in the Tree?

A) It depends on which element you are looking at.  Names and IDs probably will not change however Tagnames often will.  This is why it is recommended to first use IDs, then Names, then ClassNames.  Use Tagname last (or go up the tree a bit and grab an ID/Name then use that “branch”)

family tree 2

Q) What are the frequently used methods for selecting an Element?

A) The methods below are sorted by the most reliable/stable (not presence).  When ID is present- use it!  If no ID, Name is another great one…  Classname is also becoming more reliable

  1. getElementByID– Great because if it is present it is should always unique
  2. getElementsByName– While not required to be unique, it often is so very reliable
  3. getElementsByClassName– Often unique and present
  4. getElementsByTagName– Check if there is an ID/name above it, then jump “down” to it first then use TagName)
  5. querySelector/querySelectorAll- Most flexible but is the most complicated- (If you know CSS, this is much not hard to adapt)

Q) What are Frequently used Get/Set/Trigger HTML Elements?

A) The below are the most frequently used however this is highly tied to what your goal is.

Methods:
.click() – Clicks the element
.fireEvent(“onclick”) – Fires the click event (sometimes needed to trigger the event)
.focus() – Place the cursor in the element/edit field (set cursor)
.selectedIndex() – Dropdown box selection
.checked() – Radio / Checkbox selection

Properties (Get & Set):
innerText – the plain-text for the element
.value – the plain-text for the element
.outerHTML – the HTML for the element

Additional Resources

AHK specific:

Learning the DOM (If you spend time on this you’ll thank us later!):

  • Intro to AutoHotkey HotStrings with AutoHotkey Intermediate AutoHotkey GUIs are Easy with AutoHotkey Intro to DOS & AutoHotkey