With President Trump suddenly more antagonist than ally, House Republicans confronted the grim reality Friday that they may have no way out of an immigration mess of the president’s own making.

They prepared for a last-ditch legislative effort next week with little expectation it would succeed, as their frustrations with the president became palpable. Midterm elections loom with ­every House seat on the ballot, but the president, who will not face voters in November, airily predicted a “Red Wave” of GOP victories that few congressional Republicans expect — and told lawmakers to wait until then to deal with the immigration dilemma.

Many Republicans believe they don’t have the luxury of waiting, both because of internal House politics and because of the unfolding crisis of border separations that the president himself has insisted demands an immediate legislative solution.

“We reached, I think, a good consensus, and suddenly we wake up to another tweet,” said Rep. Dennis A. Ross (R-Fla.). “And it’s like, I think he’s with us, but he’s got to really come out for us. This is the opportunity.”

Republican leaders have struggled to rally support for a broad bill designed to unite GOP moderates and conservatives, postponing a vote twice in the face of opposition. The bill largely follows immigration principles issued by the White House in January, providing $25 billion for Trump’s long-sought border wall, scaling back legal immigration and giving young undocumented immigrants a shot at citizenship.

The bill also would allow migrant families to remain together in detention.

The mercurial Trump, who repeatedly called for Congress to act, tweeted Friday morning: “Republicans should stop wasting their time on Immigration until after we elect more Senators and Congressmen/women in November. Dems are just playing games, have no intention of doing anything to solves this decades old problem. We can pass great legislation after the Red Wave!”

Republican leaders said a vote on the bill would occur next week regardless of Trump’s wishes.

But Trump’s tweet, said Rep. Patrick T. McHenry (R-N.C.), “is very much in reality on the difficulties of immigration reform, and it also is the reality given the partisan nature of our current environment in a political year.”

Key Republican lawmakers who have been involved in the negotiations said Friday that talks would continue through the weekend. The moderates said they would explore whether they could accept some version of a key conservative priority — requiring employers to screen their workers for legal status using a federal database, paired with temporary visas for agricultural workers.

“We’ve got to decide whether we can make enough progress to make it worth our while to do it,” said Rep. Bradley Byrne (R-Ala.), a deputy whip who has been involved in vote-counting discussions. “If the president said to a given bill, ‘That’s my bill; that’s the one I want’ — I think that would have a pretty significant impact on our discussions. But he hasn’t done that yet, and so we’re continuing to work without that.”

Amid an outcry over his ­migrant separation policy, Trump traveled to Capitol Hill on Tuesday, urging lawmakers to pass either of two rival bills that sought to defuse the crisis as well as the uncertain status of young undocumented immigrants at risk for deportation because of his cancellation of the Deferred Action for Childhood Arrivals program.

The president had argued for days that only Congress could solve the border crisis. But when he instead bowed to pressure and issued an executive order reversing the separation policy, Trump undermined the GOP effort to pass a bill.

As of 4 p.m. Thursday, Trump was still making calls to Capitol Hill and demanding that the House act. Judiciary Committee Chairman Bob Goodlatte (R-Va.) relayed that message to House Republicans, which factored into their decision to continue negotiations on legislation into next week.

Then came Friday morning’s tweet.

Trump and other Republicans have accused Democrats of being unwilling to compromise because they want to use immigration as a campaign issue ahead of the November midterms. But Democrats retorted that Trump’s Friday tweet proved that it was the president who wanted to keep the issue alive for political reasons.

“What has become clear is that this president has no goal posts for what he wants to achieve on immigration because it’s actually, in his mind, a useful political tool,” said Rep. Pramila Jayapal (D-Wash.), a member of the House Judiciary subcommittee on immigration. “It says exactly who he is, which is callous, conniving, and really not focused on trying to solve any problems.”

Last year, after meeting with Trump, Senate Minority Leader Charles E. Schumer (D-N.Y.) and House Minority Leader Nancy Pelosi (D-Calif.) said they had reached a deal to protect dreamers in exchange for funding Trump’s border wall. But the deal fell apart later when the White House insisted on including other Trump priorities on immigration in the package.

Last month, Republican moderates moved to force votes on several immigration bills — including bipartisan measures that would easily pass with mostly Democratic support. But GOP leaders, who feared a conservative political backlash if a Republican House advanced such legislation, undertook a furious push to stymie the moderates.

Congress could be forced to act on immigration regardless in the coming weeks. The executive order Trump signed is contingent on the modification of a court settlement that currently requires children to be released from detention after 20 days. Several congressional aides, both Republican and Democrat, believe that such a modification is unlikely and that only legislation can ultimately address the issue.

Senate Republicans have pressed forward in crafting a narrow legislative solution to ensure families are kept together, and they have continued drawing attention to the issue.

“Ultimately, the solution here needs to be through Congress,” Sen. Ted Cruz (R-Tex.) said Wednesday, after Trump issued his executive order.

Sen. John Cornyn (Tex.), the second-ranking Republican senator, said that he hoped the Senate would come up with a legislative solution, even though Trump moved to act unilaterally.

Sean Sullivan and John Wagner contributed to this report.