By capitalizing on President Trump’s unpopularity, Wexton, a state senator and former prosecutor, won 58 percent of the vote compared to Comstock who won 42 percent, with about two-thirds of precincts reporting.

She won by portraying the congresswoman as a Trump ally who was out of touch with a well-educated, diverse electorate that has begun to shift the district to the left.

“Vote them out!” Loudoun County retiree Michele Hoehner said, capturing the mood of energized Democrats. She voted for Wexton, Sen. Tim Kaine (D) — and change, she said. “Guns are out of control. I’m tired of the meanness.”

She wanted to combat Trump’s rhetoric and was inspired by the March for Our Lives on the Mall, she said.

“I don’t like the way he talks about people, women and race,” she said of Trump. “He’s divided the country.”

Wexton dubbed the congresswoman “Barbara Trumpstock” and frequently shared a statistic from the website FiveThirtyEight that showed Comstock voted with Trump 98 percent of the time.

In response, Comstock said 82 percent of the bills tracked by the website passed on a bipartisan basis or with support from at least a few Democrats.

But Trump’s influence on the election was unmistakable. In preliminary results from a Washington Post-Schar School survey of people who voted in the 10th District, 57 percent said Trump was one of the top two factors in their vote. That is higher than 43 percent of voters in congressional battleground districts overall, according to preliminary results from the survey.

The 10th District includes Loudoun County and parts of Fairfax and Prince William counties, as well as Clarke and Frederick counties and the city of Winchester to the west.

Once reliably red, the district has moved to the middle and to the left in statewide and national races, making it more challenging for a Republicans to stay afloat, with the exception of Comstock.

Gerry Last, 59-year-old systems engineer from McLean, voted for Comstock and GOP Senate hopeful Corey A. Stewart. But he really saw it as a vote for Trump.

“The country is finally going in a good direction,” he said. “Trump is bringing back jobs. People are buying stuff again. I feel like if I want to change jobs now, there are jobs available, where before I felt like I was kind of stuck in the same position.”

Hillary Clinton won the district by 10 points in the 2016 presidential race, but Comstock also won, over-performing Trump by 16 points, with a relentless focus on local issues. At the time, Comstock called on Trump to drop out of the race after a tape surfaced with him bragging about groping women.

A year later, Democrats and anti-Trump voters dominated Northern Virginia, electing Gov. Ralph Northam and ousting all but one Republican state lawmaker who shared territory with Comstock.

Democrats felt good about their chances from the start. Polls showed Trump was unpopular in the district overall and demographics were shifting in their favor with more well-educated, diverse professionals moving in all the time.

In the final weeks, former vice president Joe Biden endorsed Wexton, and Clinton tweeted her support.

Comstock, a former GOP operative who investigated the Clintons in the 1990s, tapped friends to raise money at the highest levels of national politics, from Vice President Pence to House Speaker Paul D. Ryan (R-Wis.).

She also won the endorsement of her predecessor and former boss, Frank Wolf, former congressman Tom Davis and former Republican senator John Warner, who has just as often supported Democrats in recent years.

Not long after Trump was elected, Democrats built their campaign around harnessing 2017 voter enthusiasm and directing it at the congressional race.

In past years the party had struggled to find at least one viable candidate to challenge Comstock, but this year more than a dozen serious candidates expressed interest.

Wexton won a six-way primary on her legislative record and the strength of endorsements from Northam and Rep. Gerald E. Connolly (D-Va.) and the influential gun-control group, the Coalition to Stop Gun Violence.

“It’s appalling to see a country where kindergartners are mowed down and we still can’t talk about it,” Suri said.

In the general election, Wexton emphasized her personal story as a working mom with deep roots in Loudoun County, the heart of the district.

One ad combined video of her driving her two boys to and from school as they grew up with her accomplishments in the state Senate: “helped moms collect child support,” “targeted sex offenders,” “took guns away from domestic abusers.”

Another ad made sure voters knew Wexton voted in the General Assembly to expand Medicaid, a major longtime goal for Democrats.

Some Democrats running in more conservative districts in Virginia worried about alienating Trump voters and avoided mentioning the president, but his low approval ratings in Virginia’s 10th District made him a clear target for Wexton.

While Democrats consider Comstock too conservative for the district, some Republicans think she is too moderate and she faced a primary challenge from the right.

She won with 61 percent of the vote and turned to the general election determined to convince voters she should be judged on her own record, not that of Trump and the GOP.

Comstock, whose district includes tens of thousands of federal workers, opposed Trump’s call for a pay freeze and government shutdowns, and she voted against the Trump-backed bill repealing the Affordable Care Act.

Her ads called her an “independent fighter for Virginia” and “Virginia’s independent voice” and made the case that GOP policies stimulated the economy.

Wexton questioned Comstock’s commitment to the #MeToo movement by noting her support for her longtime friend and Justice Brett M. Kavanaugh in the face of accusations from Christine Blasey Ford during his confirmation process.

NRCC ads seized on Wexton’s vote for a budget that imposed massive toll hikes, despite her opposition to the tolling plan and accused her of reducing charges against violent criminals. The ad cited two cases where Wexton dropped charges as part of plea bargains.

None of that mattered for Courtney Riddle, a 46-year-old business owner from Loudoun County, normally votes Libertarian, but went with Wexton and Kaine this time. With Trump in office, she said it was time to “shake things up a bit.”

“At the core of it,” she said, “we need to make a statement against our administration.”