 In the general election of June 1847, he ran as a Radical free-trade candidate in Edinburgh, defeating the incumbent Whig Thomas Babington Macaulay.His initial election was declared null and void due to his being a party to a government contract, but he was re-elected in a second election that December.He was re-elected in the 1852 election in second place on the ballot, and returned unopposed in the 1857 election.