Name and ID of every text field on a web page

One of my scripts is broken because the developer now generates a unique id and unique name for each button and field every time a page is launched.

In the example below I used to be able to populate the field by using the name “Headline” but that doesn’t work. If my script has the ID it works.

But I don’t know enough javascript to workaround this.

One solution: get the name and id (and other info) for every text area, and parse that with applescript to get the ID for the one that has the field name I want (in this case “/headline”)

Or a javascript that would work on with a partial match for the field name. (field who’s name contains “/headline”)

Or a javascript that could work with the “data-dynamic-field-name”

It looks like any of those would work (although I’d have to find the same kind of workaround for various buttons too.)

Any pointers?

<textarea 
id="i34bae84fdac6424c885f30bd27f15812" 
name="0000016a-bdc0-df86-a1ee-bfc5b6230000/headline" 
placeholder="(Required)" 
data-dynamic-placeholder="${content.getHeadlineFallbackOrRequired()}" 
data-dynamic-field-name="headline" 
data-suggested-minimum-message="Too Short" 
data-suggested-maximum-message="Too Long" 
data-inline="false" 
data-user="&nbsp;Ed&nbsp;Stockly" 
data-user-id="0000016a-b279-d401-a37f-b7ffceca0000" 
data-first-draft="true" 
data-track-changes="true" 
class="bsp-onDomInsert-inserted-15 bsp-autoExpand-item" 
style="overflow: hidden; height: 40px;" 
data-previous-text="(Required)">
</textarea>

I can’t give you the js, but I suspect the XPath query string would be: "//textarea[@data-dynamic-field-name='headline']".

This should work to return the value of the text field:

document.querySelector('textarea[name*="/headline"]').value;

OK, so this is working. I’m using the partial label to grab the field ID, then using the field ID to fill in the field.

set storyHeadline to "Story Headline"
set storySlug to "Story Slug"
set storyIntialSlug to "Story Initial Slug"
set storySEO to "Story SEO"
set byLineButtonName to "addButton _ofc137 RCS-add"
set insertionInfo to {¬
   {"textarea", storyHeadline, "headline"}, ¬
   {"textarea", storySlug, "sluggable.slug"}, ¬
   {"textarea", storyIntialSlug, "sluggable.internalSlug"}, ¬
   {"textarea", storySEO, "subHeadline"}}
set AppleScript's text item delimiters to {""}

repeat with thisInfo in insertionInfo
   
   set {fieldType, newContents, fieldLabel} to thisInfo as item
   
   set jvScriptText to {"document.querySelector('"}
   set the end of jvScriptText to fieldType
   set the end of jvScriptText to "[name*=\"/"
   set the end of jvScriptText to fieldLabel
   set the end of jvScriptText to "\"]').id;"
   set jvScriptText to jvScriptText as text
   set elementId to RunJavaScript(jvScriptText)
   InputByID(elementId, newContents)
end repeat

ClickByClass(byLineButtonName, 0)

InputByClass("ComboInput", 0, "Ed Stockly")

on RunJavaScript(jvScriptText)
   tell application "Google Chrome"
      activate
      set jvScriptResult to execute (active tab of window 1) javascript jvScriptText
   end tell
   return jvScriptResult
end RunJavaScript

on ClickByName(objectName, elementnum)
   
   set jvScriptText to "document.getElementsByName('" & objectName & "')[" & elementnum & "].click();"
   set jvScriptResult to RunJavaScript(jvScriptText)
   return jvScriptResult
end ClickByName

on ClickByClass(objectClass, elementnum)
   
   set jvScriptText to "document.getElementsByClassName('" & objectClass & "')[" & elementnum & "].click();"
   set jvScriptResult to RunJavaScript(jvScriptText)
   return jvScriptResult
end ClickByClass

on InputByIDfromName(theName, num, theValue)
   set theValue to EscapeSingleQuote(theValue)
   set InputByIDfromNameJVscript to "document.getElementsByName('" & theName & "')[" & num & "].value ='" & theValue & "'"
   set jvScriptResult to RunJavaScript(InputByIDfromNameJVscript)
   return jvScriptResult
end InputByIDfromName

on InputByName(theName, num, theValue)
   set theValue to EscapeSingleQuote(theValue)
   set InputByNameJVscript to "document.getElementsByName('" & theName & "')[" & num & "].value ='" & theValue & "';" & return
   tell application "Google Chrome"
      set jvScriptResult to execute (active tab of window 1) javascript InputByNameJVscript
   end tell
   return jvScriptResult
end InputByName

on InputByID(theId, theValue)
   set theValue to EscapeSingleQuote(theValue)
   set jvScriptText to "document.getElementById('" & theId & "').value ='" & theValue & "'"
   set jvScriptResult to RunJavaScript(jvScriptText)
end InputByID

on InputByClass(theClass, theIndex, theValue)
   set theValue to EscapeSingleQuote(theValue)
   set InputByClassJVscript to "document.getElementsByClassName('" & theClass & "')[" & theIndex & "].value ='" & theValue & "'"
   set jvScriptResult to RunJavaScript(InputByClassJVscript)
   return jvScriptResult
end InputByClass

on EscapeSingleQuote(textToFix)
   set saveTID to AppleScript's text item delimiters
   set AppleScript's text item delimiters to {"'"}
   set textToFix to every text item of textToFix
   set AppleScript's text item delimiters to {"\\'"}
   set textToFix to textToFix as text
   set AppleScript's text item delimiters to saveTID
   return textToFix
end EscapeSingleQuote

1 Like

but now I have a new issue: How to insert a value in a combo box

<div class="ComboInput-label" data-placeholder="">
<div class="ComboInput-selected"></div></div>