Any way to click "Confirm" via JavaScript?

There's a page on our cart provider's site where I have to click a Confirm button to finish a task. There are many pages before that one, and on each, I was able to get Javascript to click the proper buttons for me. But I gave up on this one, and used Find Image—which works fine, but I'd like to remove the "screen is on and window visible" dependency. Here's the code around the button:

  <a href="#" onclick="mojarra.jsfcljs(document.getElementById('form'),{'form:j_idt531':'form:j_idt531'},'');return false" class="btn btn-default buttonLink button buttonLinkActionSource">
    <span id="form:j_id289" class="actionTitle buttonLinkInner">
      <span class="buttonLinkText">Confirm</span>
      <span class="buttonLinkWait">Processing</span>
    </span>
  </a>
</div>

Note that I cannot use the form's ID (j_id289 in this case), because that form ID is different each time the page loads. And there are many instances of buttonLinkText and buttonLinkInner on the page.

Any ideas for clicking that button programmatically?

EDIT: I've read about XPATH, but don't understand it enough to make use of it. I did get the XPATH to that element, but it seems tied to the form ID: //*[@id="form:j_id289"]/span[1]

-rob .

The xpath is
//span[text() = "Confirm"]

Another thing you can do also is to use ChatGPT to get answer. I ask ChatGPT to "use xpath to extract the span with Confirm text"
The answer from ChatGPT is

To extract the <span> element containing the text "Confirm", you can use the following XPath expression:

//a[@class='btn btn-default buttonLink button buttonLinkActionSource']//span[@class='buttonLinkText' and text()='Confirm']

This expression selects the <span> element with the class buttonLinkText and contains the text "Confirm".

Thanks for that…it sort of works :). I had ChatGPT write the JS bit, and it offered up this:

(function() {
    function getElementByXPath(path) {
        return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
    }
    const element = getElementByXPath('//span[text() = "Confirm"]');
    if (element) {
        element.click();
    } else {
        alert("Element not found!");
    }
})();

And while this does indeed seem to click the element—the panel with the Confirm button is dismissed—the action that takes place when I manually click Confirm is not happening. I don't have any idea why it's not doing the same thing as an actual click.

Something else I read said I could just execute the referenced action directly, but I think the problem there is that the changing ID is part of that:

onclick="mojarra.jsfcljs(document.getElementById('form'),{'form:j_idt531':'form:j_idt531'},'');return false"

So that's out, as it will be different every time.

-rob.

After looking at the form some more, I went for a brute force solution: The button is always the same distance down and left from the top right corner of the window, so I'm just using "Move and Click Mouse" instead of trying to figure out the proper XPATH/JavaScript. Works fine with the screen off, which is what I was mostly worried about.

-rob.