I think you have most of that in this Macro:
MACRO: Pause Until Web Page Element Exists [Example]
This uses the JavaScript "querySelector" rather than "XPath", which I much prefer, because it is much easier to construct and use, and can identify almost all of the same elements that XPath can.