04: Automating Chrome to Set Text & Click a button

Automating Chrome with AutoHotkey

In the fourth session with GeekDude we look at out to Chrome and AutoHotkeyautomate setting text in a search field and then hitting the button to submit the search.

Automating Chrome to Set Text & Click a button


donate to GeekDudeIf you’re loving this, please consider donating to GeekDude!

AutoHotkey script for Automating Chrome to Set Text & Click a button


#Include   ;Remember to put Chrome in your library folder

#SingleInstance,Force
;**************************************
page:=Chrome.GetPageByTitle("AutoHotkey Community","contains") ;This will connect to the second index of a specific tab
If !IsObject(page){
MsgBox % "That wasn' t object / the page wasn't found"
ExitApp
}
page.Evaluate("document.querySelector('#keywords').value ='Chrome.ahk'")
Variable =document.querySelector('#keywords').value ='Chrome.ahk'
page.Evaluate(Variable)
var:="duh"
page.Evaluate("document.querySelector('#keywords').value ='" var "'")
page.Evaluate("document.querySelector('#search > fieldset > button').value ='Chrome.ahk'")

Notes for Automating Chrome to Set Text & Click a button

00:36     Go to AutoHotkey.com/boards/

00:44     Connect to tab using Chrome.GetPageByTitle(“AutoHotkey Community”) ;the default matchtype is “starts with”

01:23     Look at page structure using right-click and Inspect.  This opends Devtools with that element selected.

01:46     It has an ID of “keywords”, copy js path.  Which will give you queryselector(“#keywords”)

02:26     Use the .value to set some text in that box.

03:00     page.Evaluate(“document.querySelector(‘#keywords’).value =’Chrome.ahk'”)

04:01     Make sure inside the JavaScript you use the “=”, not “:=”

04:15     Some people don’t want to have to learn JavaScript.  When using Chrome, you’re going to have to learn JavaScript.

04:56     When using Chrome.ahk, we’re injecting JavaScript.  So best to learn

05:54     The button is right next to the input.  You can go back to the page and right-click the button, then hit Inspect

06:13     Test the new js path.  Instead of using .value, use .click

06:42     Test in Chrome developer tool

07:18     When running an Evaluate method, it waits for the previous Evaluate to finish (so no need to sleep between them).

07:44     If you run into a problem where you think it is happening too quickly, check the forum for some solutions

08:40     Sometimes what you want to input won’t always be a static string.  If you’re trying to reference a variable, you need to use the expression syntax.  In an expression, you’re not just assigning text, you’re doing math or making function calls.

Variable =document.querySelector(‘#keywords’).value =’Chrome.ahk’

page.Evaluate(Variable)

page.Evaluate(“document.querySelector(”#keywords ‘).value ='” variablevar:=”duh”

page.Evaluate(“document.querySelector(‘#keywords’).value ='” var “‘”) “‘”)

10:48     This works because AutoHotkey splits everything up on a given line.   First is a name of a function, then says this is inside the function, then this is text inside a function.  Then builds from left to right as to the string that will be used.

12:15     AutoHotkey proceeds left to right when evaluating an expression

12:40     when you use := you’re in expression assignment mode.

13:25     With just single = you’re in plain-text mode.  It reads it as text

15:00     When automating a site, you don’t know what kind of buffer’s they have to prevent scraping / botting.

15:49     When you start automating, you might start seeing Captcha’s everywhere

16:04     Sites get really good at looking like a normal site to a user, but looking like an impenetrable fortress to code

16:36     If your variable contains a single quote or other special charachters, JavaScript will interpret it as code instead of text.

17:13     JavaScript string escape sequence will replace characters with special escape sequences

Not mentioned in Video but GeekDude wrote me after

You can escape JavaScript code using Coco’s JSON library does actually do that escaping that we discussed when talking about putting data on the page. The syntax for invoking it looks like this:

variable = 123`r`n456’quote”quote

page.Evaluate(“document.querySelector(‘#whatever’).value = ” Chrome.Jxon_Dump(variable))

The dump function will automatically escape anything that needs escaped and add quotes to anything that needs quotes.

 

 

AutoHotkey Webinar- Advanced Web Scraping with AutoHotkey

WebScraping with AutoHotkeyToday’s AutoHotkey Webinar on Web Scraping with AutoHotkey we covered some “fun” topics.   We also demonstrated using Visual Events 2 to detect Event Listeners on a page

Video Hour 1: High-level overview

Video Hour 2: Q&A

Script highlight

; Stupid simple webScraping with AutoHotkey
Send, ^a ;Select All
sleep, 50
Send, ^c ;copy
sleep, 50
Clipboard:=StrSplit(Clipboard,”:”).1 ;split the clipboard on the colon and return everything to the left of the first one
Send, ^v ;paste
sleep, 350
Send, {tab 4} ;tab to the next field where I need to re-run

We also showed a bit about dealing with Try and using .item[0] in the DOM call. i.e. instead of using

pwb.document.getElementsByClassName(“feed”)[0].InnerText
use
pwb.document.getElementsByClassName(“feed”).item[0].InnerText
This way, if an element doesn’t exist, it will not error out.

To wait for it to be present you could do this

while (pwb.document.getElementsByClassName(“feed-identity-module__stat link-without-visited-state”).length < 3){
ToolTip, Here
Sleep, 50
Cap’n Odin offered up these two gems.  The first will close all running AutoHotkey scripts and the second one will pause them.

; Close all AutoHotkey scripts
DetectHiddenWindows On
Winget, lst, List, ahk_exe autohotkey.exe

loop % lst {
hwnd := lst%A_Index%
if(hwnd != A_ScriptHwnd) {
WinClose, % “ahk_id ” hwnd
}
return

; pause all AutoHotkey scripts
DetectHiddenWindows On
SetTitleMatchMode, 2
Winget, lst, List, AutoHotkey ahk_exe autohotkey.exe

loop % lst {
hwnd := lst%A_Index%
if(hwnd != A_ScriptHwnd) {
PostMessage, 0x111, 65306, , , % “ahk_id ” hwnd
}
}

AutoHotkey Webinar- Spreading AHK Luv & Making $ with AHK

In our first hour of today’s webinar we talked through the below points on how to improve adoption rate of AutoHotkey for users as well as making money with AutoHotkey.

The second hour dove a bit deeper into both topics and discussed other subjects related to these.

Script Highlight:

FindText – Capture Screen Image & Find it

Don’t let the name “FindText” fool you (it isn’t about ‘text’)

 Things to keep in mind:

  • Make sure you keep the same: Screen resolution, magnification, font, zoom
  • Test Grayscale vs Color
  • You can adjust the size of the capture area (but smaller is generally better)
  • Tutorial 1 demonstrating the function and an Example using it
  • Function to help take action on what you find

PowerPoint deck / Resources:

Which best describes your Organization’s current RPA (Robotics Process Automation) adoption level?

Here is the full Slide deck, podcast, and book entitled: Service Automation: Robots and the Future of Work 2016 by Leslie P. Willcocks (Author), Mary Lacity (Author)

Misconceptions of RPA / Desktop Automation

Desktop Automation

  • …is only used to reduce workload & lay-off employees
  • …is mainly driven by cost savings
  • completely replaces the need for a person

Why Don’t More People Automate / Use AutoHotkey?

  • People do notlike change. (correlation with age?)
  • People don’t realize what canbe automated (“think small” – “magic redo” button?)
  • Managers / Decision makers think it is “too expensive
  • No “GUI” to use for creating scripts.  Non-techies get “overwhelmed” with coding.
  • Everyone is “too busy” to learn how to save time. Even though they’re interested in saving time they don’t find time to learn how to automate. (Story of a woodcutteris a great analogy; remember to take time to ‘sharpen the saw’)
  • Some people are too proud to let-on that they really don’t understand what you’re doing. They might try it on their own but get frustrated and give up after 15 minutes.  (Perhaps reviewing 1-1 is better than in groups?)
  • Managers oftendo not encourage their employees to automate. (I’ve had several bosses that told me NOTto automate.)
  • Most people think short-termand not long-term. This can present itself in the following dimensions
  • The job is “too small” to be automated (almost never true)
  • The job is a “1-time” need (rarely, if ever, the case; the “job” might be but the “approach” is bound to come up again)
  • I’m the only one that would use this (rarely the case)
  • It will take too long to write code to automate (I think this is one of the best things about AutoHotkey.  FAST!)
  • It must be “fully automated” (No need to automate the entire process)
  • Not considering “side-effects” of automation (tracking log to examine events, employee / customer satisfaction etc.)

Best Approach to Get Adopters

1.Don’t overwhelm them with all that AutoHotkey can do!

  • Only get them to learn 1 thing!
  • Give them time to learn/adopt that and then come back with additional tips

2.Curtail what you demonstrate to the prospect’s level

  • Luddites- Stick to HotKeys and HotStrings
  • Somewhat Nerdy- Demonstrate some scripts, Input box, Sending keystrokes / Clicks, File manipulation
  • Nerds- Whatever their need! (Dll calls, Image search, API calls, Web Scraping, Objects, RegEx, GUIs, etc.)

3.Get a senior-level advocate (Director or above)

  • Senior people see the cost-savings
  • Your supervisor will think your coding is counting against “their time”
  • Colleagues might be worried you’re “automating” them out of a job
  • IT also might try and road-block you. Get the business to “demand” it

4.Create custom script for their needs

  • Don’t build in all bells-whistles

Making $ Money with AutoHotkey

General Models

  • Selling generic programs / scripts to large audience
  • Selling custom scripts to individuals / company
  • Getting paid to code (they own the intellectual property) & you get paid by hour

Getting Paid

Licensing

Shareware / Freeware / Donations

Rich” Target markets

  • Lots of people doing same job function
  • “Thankless” / mundane jobs with clear logic/ “rules-based” descisions and structured data
  • Lots of, repetitive, transactions
  • Non-programming “business” people
  • People with Money!!!
  • Sales people!
  • Realestate

Incentives / Benefits to Highlight to Potential Clients

  • Employee / man-hour savings resulting in high ROI (Return on Investment)
  • Better quality results than from humans / Higher compliance
  • Faster deployment getting things done (clients / customers are happier)
  • Increased scalability
  • Happier, more productive, employees
  • Have more “Meta-Data” to do analysis on
  • The more you do, the easier it gets
  • The more you automate, the more time you have to automate

AutoHotkey Merchandise-White Stress ball