Use wildcard in Safari form field select

I am using KM to fill a form field in Safari. Each time the page is reloaded the ID of the form field ID changes the string of numbers inside the ID. Is there a way to use a wildcard selector with the Select Safari Field action?

example of ID change

#owner_address_attributes_usr112132300__province

turns into

#owner_address_attributes_usr282132304__province

Is there a way to parse this natively in KM? Something like…

document.forms[0][“owner_address_attributes_[ wildcard]__province”]

I think I can do it using the executive javascript in Safari but wondering if there’s an easier way within KM?

Hey James,

I think that can very likely be done with XPath, but I don’t offhand know how.

I think you can discover the tag by using something like:

var docTags = document.getElementsByTagName('*')

And iterating through them with a regular expression to find the right one – but someone more conversant with JavaScript will have to help you with that.

What I personally would do in the absence of these other techniques is to grab the source of the page and parse it with the AppleScript and the Satimage.osax.

If I found the relevant field-name then I’d proceed accordingly.

You can do something similar with pure JavaScript though:

var docSrc = document.body.parentNode.outerHTML;
var regEx = /(owner_address_attributes_usr\d+__province)/igm;
var matches = docSrc.match(regEx);
matches.join('\n');

For now try that on for size, and perhaps in time one of our better JavaScripters will chime in with something better.

-Chris

Yep, I think you're going to need JavaScript. I don't know of any native KM Actions that support wildcards like you want.

See this post:

NoteTitle: Can you use a wildcard in getElementById?
Forum: Stack Overflow, javascript

Post by: David Thomas
Post Date: 2014-03-16
Script Lang: JavaScript
Post URL: javascript - Can you use a wildcard in getElementById? - Stack Overflow

Thank you for the help.

@ccstone that pure Javascript worked like a charm. Thank you so much!

@JMichaelTX
I actually found that article earlier but couldn’t get it implemented. Thanks for the suggestion.

I don’t suppose it’s possible to pass variables from Javascript into KM?

You can't do it directly in your script, but the script can return whatever text you like into the Action's result variable:

See Execute a JavaScript in Browser action (KM Wiki).

Thank you I have it working perfect now:)

Could you please share your solution so others can benefit.

So sorry I thought I posted this with my last post!