House Minority Leader Nancy Pelosi (D-Calif.) at the Capitol in December. (Carolyn Van Houten/The Washington Post)

Intent on becoming House speaker, Nancy Pelosi is playing to a potentially historic class of newly elected Democratic women while Republican Kevin McCarthy is tacking right to win over the one man who could settle any GOP leadership fight — President Trump.

Both lawmakers are assuming the role of speaker-in-waiting as candidates nationwide battle for the House majority, moving aggressively to consolidate power and plot their first steps if their party wins control in next month’s midterm elections.

The two Californians are not only raising millions of dollars and campaigning for candidates as they work to claim Congress’s ultimate gavel next year. They are also quietly working to overcome internal challenges amid an uncertain political landscape that will not be settled until Election Day — or weeks afterward if key races remain unresolved.

For Pelosi, extending a 16-year stretch as the top House Democratic leader — and retaking the speaker’s gavel after eight years in the minority — would mean underscoring her groundbreaking status as the first female speaker and casting herself as the lone woman in Washington leadership ready to battle Trump.

“You can’t let the opposite party choose the leader of your party,” Pelosi said at a Harvard University event this week, dismissing the relentless GOP attacks on her that have prompted dozens of Democrats to keep their distance.

“And I say it especially to women, because they think women are going to run away from a fight,” she said, suggesting the criticism smacks of sexism. “But you can’t do that,” she added. “You believe in what you have to offer. Know your power.”

In an interview with the Los Angeles Times on Friday, Pelosi hinted that if she became speaker, her tenure wouldn't last for years and years, though she stopped short of providing a time limit. "I see myself as a transitional figure," she said.

McCarthy, the Republican House majority leader, has tried to beef up his right flank by focusing on an issue crucial to Trump — immigration, and in particular his proposed U.S.-Mexico border wall. McCarthy, 53, faces an open challenge from Rep. Jim Jordan (R-Ohio), a founder of the hard-right House Freedom Caucus, in his bid to succeed Speaker Paul D. Ryan (R-Wis.), who is leaving office.

Earlier this month, McCarthy introduced a bill to fully fund the wall at a cost of $23.4 billion and promised to bring it up for a vote after the election. “I believe we can get this done,” McCarthy recently said on Fox News, adding, “That’s why we will fight for it.”

Outwardly, Pelosi and McCarthy are expressing confidence ahead of the Nov. 6 elections, in a campaign season with Democrats seeing an enthusiasm and financial advantage but with recent signs of a GOP rebound.

House Majority Leader Kevin McCarthy (R-Calif.), left, at a news conference with House Speaker Paul D. Ryan (R-Wis.) at the Capitol last month. (Win McNamee/Getty Images)

Behind the scenes, both leaders feel increasingly confident that they will maintain the reins of power should their respective party comfortably prevail in the midterms. But a razor-thin majority on either side could upend their well-laid plans.

If the GOP manages to hold the majority, it will almost certainly be severely diminished from the current 23-seat margin, according to strategists from both parties. That could give leverage to Jordan, or another spoiler with a small bloc of support, who could deny McCarthy a majority in a floor vote for speaker. Trump could avert that outcome by making clear he prefers the man he calls “my Kevin.”

Marc Short, Trump’s former legislative-affairs director, said he did not expect Trump to take any overt role in the leadership fight.

“The president has expressed his fondness for Jordan and the way that Jordan defends him,” Short said. “But it’s hard to find any member of any leadership that has been more loyal to the president than Kevin.”

Trump has been following the leadership race. While visiting North Carolina for a late-August fundraiser for Rep. Ted Budd (R), Trump brought up the duel between Jordan and McCarthy in a discussion with a group of lawmakers.

“What do you think of Jim?” Trump asked Rep. Patrick T. McHenry (R-N.C.), according to people with knowledge of the conversation. McHenry — a top GOP vote-counter — told Trump that Jordan could not get 60 votes, and Trump seemed surprised. Trump has since repeated the figure to others, the people said.

Jordan has not undertaken the typical route to House leadership of traveling widely to raise money and building backslapping relationships with colleagues. Instead, he has spent much of October in Washington, questioning FBI and Justice Department officials behind closed doors and making almost daily appearances on Fox News to discuss alleged abuses by those agencies.

Even if Jordan fails to garner enough support, as most Republicans expect, he could sideline McCarthy and allow another Republican — such as Majority Whip Steve Scalise (R-La.), who has undertaken a grueling fundraising and travel schedule of his own — to emerge as top leader.

The numbers are as big a threat to the 78-year-old Pelosi, with nearly a dozen sitting Democratic lawmakers expressing opposition. The larger question, aides say, is how the Democratic candidates who have distanced themselves from Pelosi will proceed if elected.

Traditionally, members of a party are expected to unite on the House floor behind whomever prevails in an internal caucus vote. In 2017, for instance, only four Democrats opposed Pelosi on the floor after 63 voted against her in the caucus’s secret ballot. Her critics say things could be different this time, given how Republicans have made Pelosi a central figure in scores of races.

But while dozens of Democratic candidates have called for new leadership, only about a dozen have said categorically that they will not vote for her on the floor or aired TV ads opposing her. Most of those candidates are running in long-shot races, meaning if they win, Democrats could have a big-enough majority that she might not need their votes.

“Any time you make a campaign promise on an issue of significance then go back on it, that would be perilous territory to be in — especially right out of the gate,” said Rep. Tim Ryan (D-Ohio), who unsuccessfully challenged Pelosi after the 2016 elections.

No Democrat has openly challenged Pelosi for the top spot this time, and her opposition within the Democratic caucus is only loosely organized. But even some of her allies have pushed for a shake-up.

Members of the Congressional Black Caucus, for instance, want an African American Democrat closer to the pinnacle of power. Rep. James E. Clyburn (D-S.C.), a former CBC chairman who holds the No. 3 Democratic leadership post, said he has “made myself available” for speaker if Pelosi falters, but he said he did not anticipate that coming to pass.

“Cooler heads will prevail,” he said.

Still, Pelosi has left little to chance, moving quickly last month, for instance, to quash a proposed rules change that could weaken her grip on power. Meanwhile, she has asserted herself on the national stage in recent weeks, openly discussing what initiatives Democrats would pursue in the majority, including campaign finance legislation, expanded background checks for gun buyers, infrastructure funding, lower prescription drug prices and increased oversight of the Trump administration.

More quietly, she has met or spoken privately with nearly every Democratic primary winner over the past months — heartland moderates and left-wing insurgents alike. She huddled in late July in San Francisco with Alexandria Ocasio-Cortez, the hard-left candidate who unseated Rep. Joseph Crowley (D-N.Y.), the No. 4 Democratic leader and a Pelosi ally, in a June primary.

Since then, she has campaigned with Democratic candidates from California to Minnesota to Florida and left no doubt that she intends to remain as the top party leader.

“That sends a message of strength. That sends a message of unity. That sends a message that we have to get this thing done,” said Nadeam Elshami, a former top aide to Pelosi. “That’s one. But, two, never underestimate what two, three, four, five steps [ahead] she has in her head.”

In her conversations with Democrats, Pelosi has typically not asked for their votes or even mentioned the next leadership race, according to several candidates and aides familiar with the conversations. She has, they said, encouraged them to do whatever it takes to return Democrats to the majority. As Pelosi has put it in recent appearances, “Just win, baby.”

Some of Pelosi’s allies have been more aggressive. Rep. Jan Schakowsky (D-Ill.) said she has lobbied female candidates on her behalf, emphasizing Pelosi’s role as a political pathbreaker.

For Pelosi and McCarthy, the case for the speaker’s gavel starts with dollars. Pelosi announced a record fundraising haul, including $30 million for the Democratic Congressional Campaign Committee.

Already this month, McCarthy has campaigned for nearly two dozen Republican candidates in tight races and sent more than $7 million to party committees and individual campaigns.

A dinner McCarthy co-hosted last month with Vice President Pence at Trump’s downtown Washington hotel raised $15 million for the National Republican Congressional Committee and Protect the House, a McCarthy-sponsored joint committee that injects national donors’ dollars straight into the campaigns of embattled GOP incumbents.

Some of those incumbents are conservatives who balked at McCarthy’s last run for the speakership, in 2015. Now McCarthy is wooing them not only with campaign checks but floor votes on hard-line policy measures.

Before the House left Washington in late September, McCarthy scheduled a vote on a resolution disapproving of efforts such as those some local jurisdictions have pursued to allow undocumented immigrants the right to vote. This month, McCarthy announced — in an exclusive delivered to the conservative Breitbart media outlet — that he would set a vote to fully fund the border wall after the election.

Josh Dawsey, Gabriel Pogrund and Cat Zakrzewski contributed to this report.