KM + Salesforce Lightning (really an XPath fundamentals question)

I'm a back-out-of-retirement front-line customer-care associate. I bought KM 9.0.4 to improve ad-hoc integration between my new (100% Mac-workstation-based) SaaS employer's Salesforce Lightning CRM system (glacially slo-o-o-ow; plus our call-handling system will unceremoniously yank a page away and pop another one up in its place, whether you're mid-edit or not…) and various third-party tools. Daily, I and my colleagues make tens of thousands of manual clicks in this accursed CRM to get field content by mouse location, followed by copy-and-paste operations—as reps like me repeatedly assemble specific field info, and massage the content of our freeform case notes in our chosen text editor apps, as we converse & respond to customer problems in real-time; then paste the resulting loosely-formatted text structures into a) various other fields in the same CRM [I know, I know] and b) various other tools, e.g. our JIRA trouble-ticketing system.

[ideally, actual professional software developers would do formal API-based integration between the CRM and the other systems to address many of our routine flows; but in the real world, all engineering bandwidth is consumed by imperative improvements to our actual product…]

so: I want to grab Salesforce field data programmatically with KM, by specifying the name of the adjacent label text. this is the canonical structural pattern of a read-only field within the present DOM:

<div> 
 <div> 
   <span class="A">Label 1</span>   
 </div> 
 <div> 
  <span> 
   <span class="B">field 1 content</span> 
  </span> 
 </div> 
</div>

I need an Xpath expression that will find a span with class attribute "A" and text-match "Label 1", and return the text content of the following span with class attribute "B"; i.e. given "Label 1", returns "field 1 content".

[I know this involves some incantation involving siblings, and maybe ancestors… but my knowledge is utterly exhausted upon as I fall off the dangling end of that bloody ellipsis. (I've experimented with several online Xpath simulators, but they’re neither quick nor forgiving, and leave me feeling hopelessly too old and way too tired).]

Immense gratitude to anyone who can help push this old geezer over this hump.

if I can crack this, and demonstrate streamlining even just one of our process flows, there's an excellent chance the company will snap up a big whack of KM licences.

environment: Google Chrome 79.0.3945.88; MacOS Mojave 10.16.6
[edited in (failed) effort at preserving the code content and indentation]

Actually, I'd suggest that you use querySelector rather than XPath.
You can generally get the same results, but it is much easier to understand and create than XPath. But, as it turns out, in this case, querySelector can't search on the innerText of an element, so we have to use XPath.

So, for your given HTML, here's the JavaScript:

As you noted, the XPath for this is fairly complex, and there is probably a better way, but this seems to work:

var xPath = '//div/span[@class = "A" and text()="Label 1"]/ancestor::div';
var topDivElem = document.evaluate(xPath, document, null, XPathResult.ANY_TYPE, null).iterateNext().parentNode;
var spanBtext = topDivElem.querySelector('span.B').innerText;
spanBtext;

Put this JavaScript in a KM Execute a JavaScript in Front Browser action.

Please test with your actual web page and let us know if this works for you.

wow; works great—thanks so much!
your speediness in coming up with a solution is just… awesome.
I've been playing with it successfully all evening.
Your support truly makes Keyboard Maestro the bee's knees.

all the best for the holidays!