0

Sorry for my lack of understanding of HTML/JavaScript, but I'm trying to simply click a button on a webpage to submit a form. The following is the html profile of the "submit" button.

<a href="javascript:" class="btn btn-success" id="btnSave">Submit</i></a>

I'm attempting to use the following JavaScript to click on it.

document.getElementById('btnSave').click()

I understand that sometimes DOM click() doesn't always work, but I'm unable to come up with another solution or even understand why it won't.

Just for some background information, I don't own the website so I can't change the button code or the JavaScript. I'm attempting to run my JavaScript though AppleScript in Safari.

If I need to post the original page's JavaScript, I'll gladly do it.

Levi Muniz
  • 389
  • 3
  • 16

2 Answers2

0

Since you mentioned you do not have access to button code I am assuming it is an iframe and thus Assuming you have the appropriate access rights. You can use javascript to perform the button click event as I have tried in this snippet.

document.getElementById('myframe').contentWindow.document.getElementById("myBtn").click();

Please note the cross-origin restrictions imposed by browsers, which will not allow you to invoke scripts across different domains.

However, you will not be able to do this if the page in the frame is loaded from a different domain (such as google.com). This is because of the browser's Same Origin Policy.

Reference post

damitj07
  • 2,689
  • 1
  • 21
  • 40
  • So, your example worked, but not as you intended. By calling on "children", I was actually able to get the correct functionality. – Levi Muniz Feb 22 '18 at 08:27
  • It was just for reference :) as I was working on different site and children wad to access that particular element – damitj07 Feb 22 '18 at 08:40
0

I was able to solve this by using document.getElementById('btnSave').children[0].click();

I'm not exactly why this worked other than because getElementById('btnSave') probably returned multiple results, and since I no longer have access to the page I can't investigate further.

Hope this can help someone else!

Levi Muniz
  • 389
  • 3
  • 16