Fixed possible infinite loop

Fixed possible infinite loop when an id selector is used, and the
element doesn't exist.