I have tried various solutions to poll for elementID before continuing on with the applescript execution. I prefer to poll rather than to have a arbitrary delay.
set pageNotLoaded to true
repeat while pageNotLoaded is true
if (doForThisID contains "thisElementID") then
set pageNotLoaded to false
a simple Safari call for document text to look for a string.
You can alter this script to use just one of these.