Controlling a web page

whatsnew
km8

#1

Since many things you do happen in web pages these days, being able to automate them is quite useful.

The tricky bit with web pages is they tend to be quite fluid, the window size changes, and the contents of the window can move around quite a lot, and the window often scrolls, so identifying elements on the page can be quite tricky.

A good solution to this issue is using an XPath, which is like a file system path, but is the path through the Document Object Model (DOM) to the desired element.

Keyboard Maestro 8 adds support for XPath in all the actions:Browser Form Actions like Set Field to Text and Click Link. The actions will generally let you select the appropriate fields or links and fill in the XPath for you.

This lets you more robustly control web pages.

Like applications, when controlling web pages, the degree of robustness depends a lot on how the web page is implemented. If it is implemented cleanly, you can control it reliably and easily. If the developers of the web site have added lots of custom bells and whistles, then controlling it may be far more difficult, and you may have to resort to UI actions.


#2

Peter,

I would appreciate a few examples demonstrating this feature.

Thank you


#3

My most used Xpath script that I use many times a day is this one shared by @ccstone so all credit to him :

-------------------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2017/03/29 04:44
# dMod: 2017/03/31 03:14
# Appl: Safari, System Events
# Task: Open the First Hit on a Google Search
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Safari, @System_Events, @Open, @First, @Hit, @Google, @Search
-------------------------------------------------------------------------------------------

set xpathStr to "//*[@class=\\'r\\']/a"
set jsCmdStr to "

var xpathResults = document.evaluate('" & xpathStr & "', document, null, 0, null),
  nodeList = [],
  oNode;

while (oNode = xpathResults.iterateNext()) {
  nodeList.push(oNode.href);
}

nodeList.join('\\n');

"

# Allow for the possibility that Safari has been hidden.
# When Safari is hidden it does correctly discern window 1.
tell application "System Events"
	tell application process "Safari"
		if its visible is false then set its visible to true
	end tell
end tell

tell application "Safari"
	set linkList to (do JavaScript jsCmdStr in front document)
	
	if linkList ≠ "" then
		
		set linkList to paragraphs of linkList
		
		repeat with listItem in linkList
			if contents of listItem = "" then
				set contents of listItem to 0
			end if
		end repeat
		
		set linkList to text of linkList
		
		set AppleScript's text item delimiters to linefeed
		set firstLink to item 1 of linkList
		set URL of front document to firstLink
		
	end if
	
end tell

-------------------------------------------------------------------------------------------

It picks the first result from Google. Can adjust it to pick 2nd, 3rd too.


#4

I posted a macro that used XPaths to save and restore values of a form.

Also, I just posted a reply there that describes explicitly how to click on a link by copying the XPath in Safari.