Improved form submission detection code for headless drivers

Improvement to #391.