Javascript to fill in Gmail body with variable

In my Gmail, the xpath for the subject and body fields constantly changes every time I compose a new email. So the xpath is not a valid solution.
For the subject I've been able to use the 'javascript action in Safari" of:

var subjectElem = document.querySelectorAll('[name="subjectbox"]');
subjectElem[0].value = document.kmvar.Subject;

where the subject field has an input name of "subjectbox" - so good so far.
All set with the subject.
Now for the body field, which as stated, the xpath ID constantly changes.
The body does not have an input name:

but it does have an 'aria-label' which equals "Message Body"
I"ve tried various combinations in the javascript to no avail. (because at this point I"m guessing, it's well above my pay grade.)

I did find this out on the web,
browser.find_element_by_css_selector("div[aria-label='Message Body']")
but am not sure how to implement it.

Any help would be appreciated.
The goal is to populate the body of a Gmail with a KM variable, where the javascript does not rely on the xpath ID.

Chrome’s Copy As > XPath can easily give the impression that there is a single unique way of referencing an element using the XPath syntax. (‘the XPath’ as you understandably put it)

In fact, however, Chrome is just doing its best to offer an XPath (one of many different possibilities) that it hopes may be helpful – there is no one-to-one function from a given element to a unique XPath for it. As in the rest of nature, there are generally many different paths from A to B :- )

Here, for example, you might try using this XPath:

//*[@aria-label="Message Body"]

If that path ‘over-produces’ (turns out to find more than one match/destination if you use it with ⌘F in Chrome’s source viewer), then you could refine it a little until it reliably produces a unique hit.

The next step, for example, might be to test:

//*[@aria-label="Message Body" and @g_editable="true"]
1 Like

awesome, although I’m not able to ‘set safari field’ I can get the cursor to the body and then I’ll be able to paste whatever variable I need.
Would there be ‘one-fell-swoop’ technique that would go to and populate that field that you know of?
Or is the best I can do it to, go to the field and then paste?
(I do understand about tab, shift-tabbing to move the cursor, but want to hard code)

I haven’t experimented with Google Mail pages, but with more sophisticated pages like that, pasting is probably the right bet.

1 Like