I’m using a script to log on to a webpage.
Below are portions of the code. I have used a delay to log into the url but now it appears that my connection is slower and it gives me errors. My method of using delays is not foolproof. How can I change the following code to log in without errors?
to open the url
property site_url : "http://www.anyurl.com"
tell application "System Events"
delay 2
end tell
tell application "Safari"
activate
open location site_url
to log into the url
tell application "System Events"
delay 6
keystroke tab & myinfo & tab & firstname & tab & lastname & return
delay 2
keystroke tab & space & tab & return
delay 4
I am doing something similar for filling out an internal web form. Where I ran into errors was not so much if the page took too long to load, but that occasionally all the keystrokes didn’t seem to fire correctly, I assume because they executed faster than the browser could recognize. What seemed to work for me after several tests was to put a slight delay (0.25 seconds) between each keystrokes, something more like:
tell application "Safari"
delay 1.5
tell application "System Events"
keystroke tab
keystroke theNumber
delay 0.25
keystroke tab
delay 0.25
keystroke theUsername
delay 0.25
keystroke tab
delay 0.25
keystroke thePassword
delay 0.25
keystroke return
delay 2
end tell
end tell
Hi,
if the text fields and the submit button on the website are javascript based,
the most foolproof solution is using the do javascript command of Safari,
I’m going to assume you mean your problem is that because of your slow connection that your script is trying to log in before your login page loads.
here is a handler that you insert immediately after the command to load your login page. If you ever pay attention to the title bar on your browser window it first says “Untitled” then changes to “Loading” when calling a webpage. This handler waits for both of those to disappear then issues a java script to wait until the page is fully loaded before giving back control.
Try it and let me know how it works for you.
to wait4page()
delay 2
repeat
delay 0.1
tell application "Safari" to set winName to the name of window 1
if winName does not contain "Untitled" then exit repeat
end repeat
repeat
delay 0.1
tell application "Safari" to set winName to the name of window 1
if winName does not contain "Loading" then exit repeat
end repeat
repeat
delay 0.1
tell application "Safari"
do JavaScript "document.readyState" in document 1
if result is "Complete" then exit repeat
end tell
end repeat
end wait4page
Thanks for the info.
I have tried to do a variation of both on a test script. This just opens a page and logs in. It’s complicated by the fact that safari isn’t open when I start the script. I have thus tried to close the first window that opens. This is what I tried. Sometimes it works sometimes it doesn’t. I don’t know any java so that is not really an option to me.
tell application "Safari"
activate
delay 1.5
end tell
tell application "System Events"
delay 0.25
keystroke "w" using {command down}
end tell
tell application "Safari"
open location site_url
-- wait until page loaded
my wait4page()
end tell
tell application "System Events"
delay 0.25
keystroke username
delay 0.25
keystroke tab
delay 0.25
keystroke upassword
delay 0.25
keystroke return
end tell
to wait4page()
delay 2
repeat
delay 0.1
tell application "Safari" to set winName to the name of window 1
if winName does not contain "Untitled" then exit repeat
end repeat
repeat
delay 0.1
tell application "Safari" to set winName to the name of window 1
if winName does not contain "Loading" then exit repeat
end repeat
repeat
delay 0.1
tell application "Safari"
do JavaScript "document.readyState" in document 1
if result is "Complete" then exit repeat
end tell
end repeat
end wait4page