WASHINGTON — Democrats eager to thwart President Trump’s agenda and investigate his administration looked positioned to replace Republicans who have supported and protected the president, as early returns suggested Democrats were headed to retake the House majority in Tuesday’s midterm elections.

If they prevail, Democrats intend to move quickly to usher in a new era and tone at the Capitol, making a package of anti-corruption measures aimed at strengthening ethics laws, protecting voter rights and cracking down on campaign finance abuses their first major piece of legislation.

They hope to follow with legislation aimed at lowering the cost of prescription drugs and at rebuilding the nation’s infrastructure – both areas, infrastructure in particular, where they see the possibility of common ground with Trump.

“That is something he wants to do,” House Minority Leader Nancy Pelosi, D-Calif., said Tuesday evening in an interview on PBS NewsHour, referring to infrastructure. “It’s always been nonpartisan – always been nonpartisan. Hopefully, we can work together to advance that agenda.”

A Democratic House takeover would amount to major vindication for Pelosi, who became the first female House speaker in 2006, only to lose the majority in 2010 as voters rebelled against President Obama’s health care law and other priorities in the first midterm election of his presidency. This year, the midterm elections once again focused on health care, only this time Democrats were on the attack against Republicans over the GOP’s many attempts to repeal the Affordable Care Act and its signature protections for people with pre-existing conditions.

And Pelosi could be in line to ascend to the speakership once more, although that outcome is not assured given that a number of Democratic candidates distanced themselves from her in the course of the campaign.

Democrats, who must gain 23 seats to take the House, were trying to win dozens of seats around the country now held by Republicans, many of them in suburban districts where polls suggest women in particular have soured on Trump.

Three hours after polls started closing across the country, Democrats were more than halfway toward that goal, and a trend had emerged: They were meeting outsize expectations in suburban areas where Trump is deeply unpopular but struggling to unseat Republicans in more rural seats they hoped might be competitive.

In the eastern suburbs of Denver, Rep. Mike Coffman, R-Colo., lost to lawyer and former Army officer Jason Crow, while Republican incumbents in Kansas City and Minneapolis-area districts also fell to Democratic challengers. All three seats had been heavily targeted by Democrats in 2016, but this year was different.

In northern Virginia, Republican incumbent Barbara Comstock lost to Democrat Jennifer Wexton, while in one surprise win, Democrat Max Rose – a former boxer who raised millions on the strength of a viral video – unseated Rep. Dan Donovan, R-N.Y., in a seat encompassing Staten Island and Queens. Donovan was the only Republican representing New York City.

In a disappointment for the Democrats, incumbent Republican Rep. Garland “Andy” Barr held off a strong challenge from Democrat Amy McGrath, a former fighter pilot. McGrath raised millions in small donations and rocketed into contention thanks to a viral video introducing her candidacy, but her bid fell short in a district where Trump beat Clinton by 15 percentage points.

Polls remained open in a few West Coast states and many votes remained to be counted, making the ultimate outcome uncertain.

Heading into Election Day, though, even Republicans said their best-case scenario after Tuesday’s voting was a narrower House Republican majority than the 45-seat margin they now command. Republicans had pledged that, if returned to power in the House, they would get to work on a new 10 percent tax cut for the middle class Trump spoke of in the closing days of the campaign.

“We’ve known from the beginning that history was not on our side this election cycle. And the big money was not on our side,” House Majority Whip Steve Scalise, R-La., said, predicting a five-seat Republican margin early Tuesday, citing a “motivated base” on the Democratic side that inundated Republican incumbents with small donations to their challengers.

The Senate looked likely to remain in Republican hands, which would put a damper on ambitious Democratic plans in the House and could make gridlock the order of the day. That could mean an impasse on issues including immigration, guns and health care – the topic that, more than any other, defined House races around the country this campaign season.

Whichever party ends up with the House majority, both the Democratic and Republican caucuses are facing potentially messy struggles to determine who leads them into Washington’s next chapter. And each party faces internal ideological differences that would complicate their ability to enact a unified agenda.

Pelosi would be the front-runner for speaker with Democrats in the majority, but a small group of skeptics inside the House Democratic Caucus has long sought to undermine her, citing the political drag she has represented on candidates running in swing and Republican-leaning districts. Republicans this year aired tens of millions of dollars worth of ads attempting to link Democratic candidates to Pelosi and a left-leaning policy agenda.

Many of those candidates have spoken out against Pelosi, either by calling for new leadership or by specifically saying they would not vote to install her as speaker if Democrats win a majority.

“We don’t want to be disrespectful to anyone who served, particularly a woman who’s broken glass ceilings like Nancy Pelosi,” Michigan candidate Elissa Slotkin said Tuesday. “But we need to hear what people are telling us, and they’re saying on both sides of the aisle they want a new generation of leadership. . . . I’ve said it because I believe it, and I’m not going to flip the minute I get to Washington.”

If Democrats retake the majority, it will be thanks to many moderate candidates who beat Republicans in districts that voted for Trump. But the party would welcome newcomers who ran on distinctly progressive agendas calling for Medicare-for-all or abolishing the Immigration and Customs Enforcement agency – such as New York’s Alexandria Ocasio-Cortez, who beat longtime Democrat Joseph Crowley in a June primary, and Michigan’s Rashida Tlaib, who is set to claim the seat once held by veteran lawmaker John Conyers Jr. in a deep-blue district.

That mix would be certain to create tensions over the party’s priorities, especially with a restive liberal base that has already begun calling for impeachment proceedings against Trump.

On the GOP side, with House Speaker Paul Ryan, Wis., retiring from Congress, Majority Leader Kevin McCarthy, Calif., is his likeliest successor as the top Republican leader in the majority or minority. But he may not get there without a fight, since Scalise is also eyeing the job, and Rep. Jim Jordan, Ohio, a leader of the House Freedom Caucus, is the choice of some conservatives.

If surprising late results leave Republicans in control of the House but with a smaller margin, the Freedom Caucus – a bloc of conservatives that has proved difficult for successive leaders to control – could gain even more clout, pulling House Republicans to the right and making dealmaking with Democrats and with the Senate even tougher.

Tuesday’s voting capped intense House races around the country that saw many Republicans on defense over health care. Republicans who rode their opposition to Obamacare to the House majority in 2010 were forced to backtrack in race after race, insisting that they actually did support such protections.

Despite the strong economy and Republicans’ success in pushing a $1.5 trillion tax cut package into law, those achievements were not central to many GOP campaigns. Instead many Republicans followed Trump’s lead in raising fears about illegal immigration and crime, while casting Democrats as overly liberal and linking them to Pelosi.

“They still have a big internal struggle within their ranks, win or lose,” Scalise said of Democrats. “There are a lot of Democrat members that don’t like Nancy Pelosi’ agenda. They’re just afraid to take it on. And I think they’re going to have to confront their internal struggles as soon as this election is over.”

Only subscribers are eligible to post comments. Please subscribe or to participate in the conversation. Here’s why.

Use the form below to reset your password. When you've submitted your account email, we will send an email with a reset code.