Add waitForValid() function to poll until element becomes available