Searching Text On Webpage

Hey all,

So I’m trying to navigate an internal work site. It has a couple popup windows, and a few disclosure triangles to expand before you are presented with a list of names/checkboxes. I’ve already got a script working to get me to the list. But finding a name in this list and checking the respective box has proven difficult. There are over 1100 rows of names (in alphabetical order if that matters), and my initial proof of concept script was along the lines of:

tell application "System Events"
	tell process "Safari"
		set frontmost to true
		set ManagerName to "Example ManagerName"
		
		repeat with rowNumber from 1 to 1150
			try
				click static text ManagerName of group 1 of group 2 of group 1 of group rowNumber of group 2 of group 1 of group 2 of group 1 of group 2 of group 12 of group 1 of group 1 of group 6 of group 38 of UI element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 of window "Workforce Optimization"
				
				exit repeat
			end try
		end repeat
	end tell
end tell

However, this would take way too long to find people lower down the list. I would hardcode my list in for the row numbers to match, but the row numbers change daily as names are added and removed. :frowning:

So my next thought was simply CMD F search for the names and then have it take me to the name and I can manually check the box myself. However, for whatever reason the CMD F will find the name on the page, but won’t take you to the result it found; it merely highlights the name that matches and you’ll see it once you scroll down and find it. Is there anyway to have AppleScript scroll down so that the website search result is visible? OR would there be a more reliable way to find the people and check their box? Maybe a way to first dump the list of names so their row number could be found ahead of time?

Any ideas would be greatly appreciated.

Without access to the page, it’s hard to try to hack up anything.

The first thing I’d try would be getting the page source:


tell application "Safari" to set htmlSource to the source of the document of front window

Then use Applescript to find the name in the source. Then the (possibly) tricky part is constructing the correct “click static text ManagerName” based on parsing the source. But if you can get it that way, it should be fast.

  • Tom.

IME, you would be much better off using JavaScript injection into Safari for this type of task. There are many JavaScript and DOM functions designed to do exactly this type of navigation/search. These tools are much, much better than the AppleScript System Events UI tools.

To get started, I’d suggest searching the Internet for “AppleScript Safari JavaScript” (without quotes). If you don’t find what you need there, you might try this search in the Keyboard Maestro Forum:
KM Forum Search: Safari JavaScript

You don’t have to use KM to use the JavaScripts you will find there, often referenced as “Execute JavaScript in Safari”. Just use the JavaScript in AppleScript. Navigating and scraping web pages is one of the often uses for KM and JavaScript.