In this context, this phrase means that somebody hoped into the good end, even when all hopes have been lost.

Of course the phrase "хвататься за соломинку" sounds more habitually, but poets very often tend to rephrase stable expressions into suitable ones.