How to trigger button click event in iframe using javascript

I am working on an application where I need to trigger button click events using javascript.

window.onload = function() {

  // set up event listener on Pay With Card button
  function StartTransaction() {

    var urlParams = getUrlVars();
    var transToken = urlParams["transactionToken"];
    externalTransId = urlParams["externalTransId"];
    var urlPage = emergepayUrlPage.init({
      // (optional) Callback function that gets called after a successful transaction
      onTransactionSuccess: function(approvalData) {
        transactionExecuted = true
        clearTimeout(pollingTimeout);
        postCallbackCPResponse("true", approvalData);

      },
      // (optional) Callback function that gets called after a failure occurs during the transaction (such as a declined card)
      onTransactionFailure: function(failureData) {
        transactionExecuted = true
        clearTimeout(pollingTimeout);
        postCallbackCPResponse("false", failureData);
      }
    });
    // set the source of the iframe to the emergepay page.
    iframe.src = urlPage.getUrl(transToken);
    iframe.style.display = 'block';
    $(".iframeContainer").show();

    //var pollingTimeout = setTimeout(function () {
    //    if (!transactionExecuted) {
    //        startServerPolling();
    //    }
    //}, 3000);

    iframe.onload = function() {
      // Access the iframe document
      const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;

      // Find the submit button within the iframe, for example by its ID
      const submitButton = iframeDoc.querySelector('button[type="submit"]');

      // Trigger a click on the submit button if it exists
      if (submitButton) {
        submitButton.click();
      } else {
        console.log("Submit button not found in the iframe.");
      }
    };
  }

  StartTransaction();
}