Click "Submit" button in Safari

Have tried many different variations of:

tell application "Safari"
	activate
	--do JavaScript "document.getElementById(˜submit.button˜).click();" in document 1
	--do JavaScript "document.getElementsByTagName(˜" & thetagName & "˜) [" & elementnum & "].click();" in document 1
	--do JavaScript "document.getElementById('button#validate-button-id.btn.btn-primary.btn-block').click()"
	--do JavaScript "document.getElementsByTagName(˜Submit˜).click();" in document 1
	do JavaScript "document.getElementById('validate-button-id').click" in document 1	
end tell

No luck. The button code is:

<div class="col-xs-4 col-sm-4 col-md-4">
				<button id="validate-button-id" type="button" class="btn btn-primary btn-block" style="color: #eee;text-shadow: 0 0 0;padding: 4pt;padding-left:8pt;padding-right:8pt;">Submit</button>
</div>

I may just have to call the function which is called when the button is clicked but I can’t even see that!

It’s just a button that’s doing nothing. Your script probably works but no function is or that we can see is assigned to the onClick event. Some sites doesn’t have submit buttons in their forms but outside it and the called function assigned after the dom is loaded. So when the DOM is loaded (the source code you’re looking at) it just looks like an orphaned button. The advantage is that most spider software that is grabbing web contents and fill forms with spams won’t work on those forms. You have to look deeper into the code and find what function or line of code is called when the user presses the button. Probably an event is added as event listener to your button after the DOM is loaded.

What you may be looking for is something like:

[format]document.getElementById(‘validate-button-id’).addEventListener(“onmouseup”, [funcion name]);[/format]

Keep in mind that this can be done by CSS of course.

But I have seen more complex tricks in the past. To keep spider software away, the parent element, in this case the div, contains an event listener. The button will behave the same way (press state) but the click is handled by another element and your attempt won’t work. If the parent div has actually an event listener you may need something like this:

tell application "Safari"
   activate
   do JavaScript "document.getElementById('validate-button-id').parentElement.click()" in document 1    
end tell

Cheers DJ.

No amount of cursing and combos helped.

So, off to accessibility inspector and the rather time consuming:

tell application "System Events"
	click UI element 9 of UI element 1 of scroll area 1 of group 1 of group 1 of tab group 1 of splitter group 1 of window "bob" of process "Safari"
end tell

Once you get to UI element 1, System events doesn’t understand anything other than UI Elements, which makes it slightly easier once you know that!

So did you find the accessibility inspector useful, or even essential, in figuring out the above statement?

Have you ever used the UI Browser? If so, how would you compare it to the accessibility inspector?

Kind of… Useful in knowing how many levels I needed to go down. Useless once it got to the actual web page.

What I did, instead, was to start with a simple:

tell application "Safari" to activate
tell application "System Events"
	tell process "Safari"
		UI elements	
	end tell
end tell

This returns:

then repeating the process for every element that Accessibility Inspector told me about, resulting in the rather crazy:

tell application "Safari" to activate
tell application "System Events"
	tell process "Safari"
		tell front window
			tell splitter group 1
				tell tab group 1
					tell group 1
						tell group 1
							tell scroll area 1
								tell UI element 1
									UI elements
								end tell
							end tell
						end tell
					end tell
				end tell
			end tell
		end tell
	end tell
end tell

Using the Lock feature of AI told me that it was UI element 9

A shame that Safari doesn’t understand the UI elements within a web page, but fully understandable at the same time

Woggledog,

can you provide the URL you want to click the button. We cannot help you any further this way because I believe it still can be done using JavaScript, and should be done that way.

Safari itself is nothing more than an application with a very large custom C++ written view (originally kHTML). Like image and video views the HTML view is a similar view that contains no object from the Cocoa API.