Click on a checkbox

Hi! I am using

tell document 1 of application "Safari"
    do JavaScript "document.getElementsByClassName('classname')[0].click();"
end tell

to click on a checkbox, but would it be possible to click only when the checkbox is blank? How can I do it?

Model: iMac Pro
AppleScript: 2.11
Browser: Safari 537.36
Operating System: macOS 10.14

Try this:


tell document 1 of application "Safari"
	do JavaScript "if (!document.getElementsByClassName('classname')[0].checked) {document.getElementsByClassName('classname')[0].click();}"
end tell

or set the checked state direct:


tell document 1 of application "Safari"
	do JavaScript "document.getElementsByClassName('classname')[0].checked=true;"
end tell

Hi, it doesn’t work in both cases.

In the first one, you always click on the checkbox regardless of its status: activated or deactivated (blank).

It does the same as:


tell document 1 of application "Safari"
do JavaScript "document.getElementsByClassName('classname')[0].click();"
end tell

The second case does not work.

I have checked https://www.w3.org/TR/selectors-4/#checked but can’t think of any solutions.

Ok. In my case it worked and the property “checked” is also the correct one:
https://www.w3schools.com/jsref/prop_checkbox_checked.asp

Maybe it is not a default checkbox. Then I have no more idea.

Edit:
Maybe the checkbox has a “value” property. Then you could check it for true/false or 0/1.

I think the problem is that in the specific url the function “getElementById” or “getElementsByClassName” returned a wrapper and not an HTML Input Checkbox object.
Click works, but the object has not a checked property (otherwise frederik’s and my code would work).
For Example: If the url uses a library like ExtJS, then a checkbox object has a get- and setValue function instead of the checked property.

Without knowing the specific url it is therefore difficult to give help.

(Sorry for my bad English)

Maybe an explicit comparison using the == operator and splitting the process into 2 steps would help solve the OP’s problem. Anyway, trying won’t hurt:


tell application "Safari"
	set isNotChecked to (do JavaScript "document.getElementByClassName('classname')[0].checked == false;" in document 1)
	if isNotChecked then (do JavaScript "document.getElementByClassName('classname')[0].click()" in document 1)
end tell

If that doesn’t help, then the OP’s ASP-page simply doesn’t implement a checkbox state return mechanism, as db123 said in the last post.

In this case, the last chance is to check state of other HTML-elements that change according to the state of the checkbox.

The similar code applied to the https://www.w3schools.com/howto/howto_js_display_checkbox_text.asp worked for me too:


tell application "Safari"
	activate -- to see visually
	delay 1 -- to see visually
	set isNotChecked to (do JavaScript "document.getElementById('myCheck').checked == false;" in document 1)
	if isNotChecked then (do JavaScript "document.getElementById('myCheck').click()" in document 1)
	delay 5 -- to see visually in the Script Debugger
end tell

This is the code for the checkboxes where the first example has the checkbox checked and the second does not:

Testing - in visual mode too - I always get “the variable is not defined”.

I have checked all the code well but the error is always the same.

I have checked this with Your HTML-Code:

You have more than one checkbox with the same class name and the checkboxes have no Id.
Therefore, you must use the class name (‘bs-checkbox’) and either repeat the command with the corresponding index of the checkbox you want set to checked:


tell document 1 of application "Safari"
	do JavaScript "if (!document.getElementsByClassName('bs-checkbox')[0].checked) {document.getElementsByClassName('bs-checkbox')[0].click();}"
	do JavaScript "if (!document.getElementsByClassName('bs-checkbox')[1].checked) {document.getElementsByClassName('bs-checkbox')[1].click();}"
end tell

or you make this with a loop for all checkboxes:


set theScript to ¬
	"var cb =document.getElementsByClassName('bs-checkbox'); 
	for (i = 0; i < cb.length; i++) {
		if (!cb[i].checked) {
			cb[i].click();
		}
	}"

tell document 1 of application "Safari"
	do JavaScript theScript
end tell

PS: The default state of the first checkbox is “checked”. Maybe your code never worked.
You can check the example by saving the HTML code to an .html file and opening it in Safari. Then, execute the Applescript code.

Hi, PepeCarvalho.

I see your HTML, and now I think, the following code should check your checkbox correctly. Test it, please:


tell application "Safari"
	try
		set isChecked to (do JavaScript "document.getElementByClassName('bs-checkbox')[0].checked == \"checked\";" in document 1)
		if isChecked is false then do JavaScript "document.getElementByClassName('bs-checkbox')[0].click()" in document 1
	on error
		do JavaScript "document.getElementByClassName('bs-checkbox')[0].click()" in document 1
	end try
end tell

NOTE: maybe, the if statement no need at all.