not sure what you mean by "if on layer" but why bother with that part? if the layer with the "yes" button is not visible, it won't be clicked. so just:
press "yes" restart layout
set layer "restart" invisible
(I'm not sure why, but in my testing, I had to also turn the layer visibility back off along with the restart. I guess restart layout doesn't restart layer visibility?)