By-elections were seen as an indicator of the two main parties' prospects at a time when voters are struggling with high inflation, strikes and rising mortgage rates.