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
Now run this code, it will alert the status of checkbox in boolean form.
set theScript to "function myFunction() {
var x = document.getElementById('myCheck').checked;
alert(x)
}
myFunction()
"
tell application "Safari" to do JavaScript theScript in document 1
Now we could make handler to use parameter of HTML id attribute
return booleanStatusCheckbox("myCheck")
on booleanStatusCheckbox(_id)
set theScript to "function myFunction() {
return document.getElementById('" & _id & "').checked;
}
myFunction()
"
tell application "Safari" to do JavaScript theScript in document 1
end booleanStatusCheckbox
If you like to have a dialog when the checkbox is false
set status to booleanStatusCheckbox("myCheck")
if (status is false) then return display dialog "Checkbox is " & status
on booleanStatusCheckbox(_id)
set theScript to "function myFunction() {
return document.getElementById('" & _id & "').checked;
}
myFunction()
"
tell application "Safari" to do JavaScript theScript in document 1
end booleanStatusCheckbox
So now you could make your if else statement to click only if the status of boolean is false.
ex.
(*
Demo url: https://www.w3schools.com/howto/howto_js_display_checkbox_text.asp
*)
set status to booleanStatusCheckbox("myCheck")
if (status is false) then
clickCheckbox("w3-check")
return display dialog "Checkbox is " & booleanStatusCheckbox("myCheck")
end if
on booleanStatusCheckbox(_id)
set theScript to "function myFunction() {
return document.getElementById('" & _id & "').checked;
}
myFunction()
"
tell application "Safari" to do JavaScript theScript in document 1
end booleanStatusCheckbox
on clickCheckbox(_id)
set theScript to "function myFunction() {
return document.getElementsByClassName('" & _id & "')[0].click();
}
myFunction()
"
tell application "Safari" to do JavaScript theScript in document 1
end clickCheckbox
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.
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.
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
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.
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