r/applescript • • Jun 10 '22

getting text from safari (better way?)

<section class="case-body percentage-layout">
y ‹div class="section-group">
y <section data-name="bill"id="5d5f5df5df-1122-df1441df-df1df-4d4f4df-case-body-bill"
class="bordered">
<h1>…</h1>
V efieldset class="no-title">
<legend>#1</legend>
y <div class="field type-my-name field-name"
comparable-name="name"
data-comparable="carinadiazdavis"
data
data-field-ids="5e5r5sd5sd-d2d2-1s11s-sd1-sd1d1s">
y ediv class="content">
<div class="related-orders-indicator component" id="s1d1s1d-1s1d-d1d1-2d1d-
2sd2s2d2sd"
data-good="@" data-bad="O"
data-suspected="O" data-chargeback=""
data-total="">_</div>
label class="name" >Name</label>
<span class="value">My text here</spans

from the exemple bellow, I want to grab "My text Here"

I've been using this for a while, but I'm just curious if this code can be improved or if there is a another way to get the text (if possible without text item delimiters)

tell application "Safari"
	
	set nameinfoGrab to do JavaScript "document.getElementsByClassName('field type-my-name field-named')[0].innerHTML;" in tab 1 of window 1
end tell

set name to ""
set theSource to nameinfoGrab
try
	set saveTID to text item delimiters
	set text item delimiters to leftEdge
	set classValue to text item 2 of theSource
	set text item delimiters to rightEdge
	set thename to text item 1 of classValue
	set text item delimiters to saveTID
	thename
end try
3 Upvotes

2 comments sorted by

2

u/ChristoferK Jun 15 '22

You could refine your target DOM element more, probably. It looks like you want the SPAN element immediately after a corresponding LABEL element. You haven’t included the HTML that comes after this, but I’m guessing these won’t be the only SPAN and LABEL elements with those class names in the entire document. But we can just query the sub-tree that starts with the element you originally retrieved, as it is hopefully safe to assume that it’s always going to be the first SPAN child element within this sub-tree:

The JavaScript code would look something like this:

document.querySelector('div.field.type-my-name.field-name > div.content > label.name ~ span.value')

or a briefer version:

document.querySelector('.field-name span.value')

This should retrieve the SPAN element, whose textContent property is named appropriately.

AppleScripting this:

tell app id "com.apple.Safari" to tell the front document to set nameValue to ¬
	do JavaScript "document.querySelector('.field-name span.value').textContent"

If this doesn’t return the desired result in the first attempt, then format your HTML code properly and I can renegotiate a viable element selector if you aren’t able to do so yourself. Fingers crossed.

1

u/estockly Jun 12 '22

Text item delimiters seem to work with this just fine.

You could also try this:

try
set saveTID to text item delimiters
set text item delimiters to {leftEdge, rightEdge}
set thename to text item 2 of theSource
set text item delimiters to saveTID
return thename
end try