Hi all, I’m trying to write a script to access my router’s admin page and turn off wireless access. So far, I’ve been able to enter the username and password and get to the admin page. However, I can’t figure out how to tell it exactly which radio button I want it to click. Using UIElementInspector, I get the following info:
and based on that, I tried writing the following code:
activate application "Safari"
tell application "Safari"
open location ("http://192.168.1.1")
end tell
delay 2
tell application "System Events"
tell process "Safari"
set value of text field 1 of sheet 1 of window 1 to "username"
set value of text field 2 of sheet 1 of window 1 to "password"
delay 2
click button 1 of sheet 1 of window 1
delay 2
click radio button 1 of group 1 of UI element 1 of scroll area 1 of group 1 of window 1
end tell
end tell
… which compiles and runs, but gives the following error message:
The radio button’s group name is “wirelessStatus”… not sure that could be useful in scripting this or not. I’m new to this.
I’m not sure if that “Yep!” meant you fixed that and got it working or not, but here’s my solution.
Here’s the code you need to launch, and instructions on what to do afterward. Enjoy!
(*
USAGE GUIDE:
1. Open the proper website in Safari. Make sure its tab is open and its window in front.
2. MAKE SURE YOU ARE FINE WITH LOSING CHANGES!!! The contents of textboxes and other elements will NOT be spared in this process.
3. Run the script.
4. The various controls on the webpage should now be labeled "0.1" or otherwise. This, translated to JavaScript, is "document.forms[0].elements[1]". Note that with password fields, you will need to experiment, although the elements of the page should line up in numerical, logical order.
5. An example is below the source. Enjoy, and feel free to use this for ANY page scripting!
*)
tell application "Safari"
set formIndex to do JavaScript "document.forms.length" in document 1
repeat with myForm from 0 to (formIndex - 1)
set elem to do JavaScript "document.forms[" & myForm & "].elements.length" in document 1
repeat with myIndex from 0 to elem
do JavaScript "document.forms[" & myForm & "].elements[" & myIndex & "].value = '" & myForm & "." & myIndex & "'" in document 1
end repeat
end repeat
end tell
(*
EXAMPLE to open a Wikipedia article:
tell application "Safari"
make new document at front of documents
activate
set URL of document 1 to "http://www.wikipedia.org/"
delay 1.5 --fakey delay period
do JavaScript "document.forms[0].elements[0].value='Wikipedia'" in document 1
do JavaScript "document.forms[0].elements[2].click()" in document 1
end tell
*)
You would have to play with it a bit to do what you want, but I do know that radio buttons accept the “elementname.click()” syntax.