The bill to stop Boris Johnson crashing the UK out of the EU without a deal has been passed by MPs, in a further humiliation for the prime minister.

The crucial legislation – to force the UK to seek a Brexit delay if no fresh agreement has been struck by 19 October – now heads to the House of Lords for a marathon sitting.

Later, the prime minister faces another defeat, when MPs are expected to refuse to grant him the snap election he wants as his escape route from the crisis.

The bill was given a third and final reading by 327 votes to 299, a majority of 28 – one fewer than an earlier second reading division.

Hilary Benn, its Labour sponsor, immediately urged Mr Johnson not to try to wriggle out of what the Commons had decided, saying: “The House has spoken this evening.

“This House expects him to uphold the law and to fulfil the obligations that will be placed upon him to prevent the UK from leaving the European Union on 31 October without a deal.”

Protesters take to streets after Boris Johnson suspends parliament Show all 11 1 /11 Protesters take to streets after Boris Johnson suspends parliament Protesters take to streets after Boris Johnson suspends parliament Anti Brexit protesters march on Whitehall EPA Protesters take to streets after Boris Johnson suspends parliament Many of the protesters carried placards AP Protesters take to streets after Boris Johnson suspends parliament A demonstrator wears a Boris Johnson mask during a London rally AFP/Getty Images Protesters take to streets after Boris Johnson suspends parliament An anti-Brexit demonstrator wields an EU flag during a London rally. AFP/Getty Images Protesters take to streets after Boris Johnson suspends parliament The protesters also marched to Trafalgar Square, where they blocked traffic AP Protesters take to streets after Boris Johnson suspends parliament John McDonnell addressed crowds in Whitehall. PA Protesters take to streets after Boris Johnson suspends parliament A small group of Brexit supporters attempted to stage a counter protest in central London. AP Protesters take to streets after Boris Johnson suspends parliament Three people were arrested in London, including a woman who sat on the road near Trafalgar Square. AP Protesters take to streets after Boris Johnson suspends parliament In Oxford, locals and students gathered outside Balliol College, Boris Johnson's alma mater, to protest. PA Protesters take to streets after Boris Johnson suspends parliament Thousands of people gather in central Glasgow to protest against the prime minister. AFP/Getty Images Protesters take to streets after Boris Johnson suspends parliament Jeremy Corbyn spoke to protesters in central Glasgow. Getty

Minutes earlier, in bizarre circumstances, an amendment to give MPs another chance to vote on Theresa May’s thrice-rejected Brexit deal was passed “by accident”.

The amendment, tabled by Labour’s Stephen Kinnock, would have been heavily defeated, but the Conservatives failed to put up “tellers” to count the “no” votes – deliberately, most MPs believed.

It requires the government – if an Article 50 extension is secured – to use it to seek to pass a version of the failed deal, but it would still be heavily defeated in the Commons.

The defeat came on a testing day for Mr Johnson, who put in a disastrous performance at prime minister’s questions and endured fierce criticism at a meeting of Tory MPs.

He faced a growing backlash over his “night of the long knives” decision to expel 21 rebel Tories who backed the bill being brought forward – including heavyweights Ken Clarke, Philip Hammond and Rory Stewart.

At a meeting of the backbench 1922 committee, Mr Johnson was accused of blaming his chief whip for the decision, as he faced calls for a U-turn to resurrect their careers.

The voters also appeared to be turning against the hardline tactics, with 45 per cent viewing the decision to kick out the MPs as “undemocratic”, with only 32 per cent supporting it, a snap YouGov poll found.

The same survey also found the PM's decision to suspend parliament for five weeks was seen as undemocratic by a similar margin of 46 per cent to 32 per cent.