问题
Run Down
Hello, this is a very specific and situation question. Basically, I'm running Applescript in Automator on Safari, and the result is Null.
What I Have
I currently have a script that can do these things:
Before I even run the Automator program, I have Safari open
Next I run the Automator, which starts with this code:
on run {input, parameters} tell application "Safari" to activate tell application "System Events" keystroke "t" using command down end tell return input end run
This script will activate Safari, and open a new tab.
Next I have an Automator pause. This will give the page time to load
After the Safari page has opened, I run this code:
on run clickID("75610556") end run on clickID(theId) tell application "Safari" do JavaScript "document.getElementById(‘” & theId & “‘).click();" in document 1 end tell end clickID
This script should of selected the button with the ID "75610556", but it did not. This is the key problem with my program.
Next I run the following code:
on run {input, parameters} tell application "Safari" to activate tell application "System Events" keystroke "w" using command down end tell return input end run
This closes the current tab.
Next, I loop through with an Automator loop.
What I Would Like Help With
I will address my questions for the program step by step.
For step one, is this a good way to set things up? Is Automator even a proper way to go?
Is this code efficient? Any errors yet?
I really hope there is an alternative to this. I have not found one.
Why won't this work in Safari? I feel like I have done this right.
Same as number 2, just curious if this is the best way to handle this
I think this is a good way to do things.
Why I Need This + Every Single Detail (Optional Section)
These are extra details if you want them. It will give you nearly every detail, and explain why I need this.
So there is a business contest at my college. I'm a freshman programmer, and have programmed a very simple mobile networking game. The issue is there is a cash prize foe $3,000 on a poll website. This website is Poll Everywhere. The judges did not realize that this website is not secure in the slightest. We reported it, and the judges thought it was not good enough of a reason to cancel the poll and maybe search for a new alternative. We reported that Poll Everywhere can be manipulated to work to any teams advantage who was smart enough to:
A. Turn off cookies for their browser
B. Use a private browser
But of course, the judges did not listen. They thought that if somebody stayed up all night, it might change it, but during a 2-3 hour period, it will have little effect. To deter their ruling, I would like to make an example program for them on my Mac that will blow their minds: A voting program. This program will open safari. Safari has already been configured to open the voting page in private browser mode. All my program has to do is open a new tab, find the button ID, and click on it. The faster it does this the better.
- Final note: Was the back story too much? I know Stack overflow has a "spare the details" policy. I thought the elaborate version might help, but if it is a bad thing I will remove it.
回答1:
Unfortunately I don't have a lot of time to answer all your questions as it's late and I need to get to bed, but here is an example of all you need to do. No need for automator, that just seems to complicate your process to me.
You may want to tweak some of the sleep
settings a little if you need more "wait" time, I tried this on my local web server and it worked, but it's local so the site loads very fast.
on run
set i to 1
repeat until i > 5
do shell script "sleep 2"
tell application "Safari"
activate
tell window 1
set properties of current tab to {URL:"http://yoururl.com"}
end tell
end tell
do shell script "sleep 3"
tell application "Safari"
tell document 1
do JavaScript "document.getElementById(\"75610556\").click();"
end tell
quit
end tell
set i to i + 1
end repeat
end run
Also note, you'll need to change the url to your url where it says "http://yoururl.com" and you'll probably want to change the 5 in repeat until i > 5
to a larger number to let it repeat more.
来源:https://stackoverflow.com/questions/36589438/learning-applescript-trouble-linking-to-safari