State Sen. Jennifer T. Wexton beat five Democrats in the race to challenge U.S. Rep. Barbara Comstock (R) in what will be one of the most closely watched midterm elections in the nation.

Wexton won about 42 percent of the vote, besting her nearest rival, anti-human-trafficking activist Alison Friedman, by almost 20 points, in Virginia’s 10th Congressional District, unofficial results show.

Wexton, the establishment favorite, ran on her legislative record and the strength of endorsements from Gov. Ralph Northam (D) and Rep. Gerald E. Connolly (D-Va.) as well as the Coalition to Stop Gun Violence. A former federal prosecutor, she is the only candidate from Loudoun County, the heart of the district.

“I’m Jennifer Wexton, and I’m going to repeal and replace Barbara Comstock,” she told voters and staff at her victory party. “If tonight showed us anything, it’s that people are ready for change.”

Supporters surrounded by blue and white balloons at a pub in Sterling chanted “Jennifer! ­Jennifer!”

“Now is the time when we all have to pull together,” Sen. Tim Kaine (D-Va.) said before introducing her. “We are going to send a message of hope this November.”

In the Republican primary, Comstock won around 61 percent of the vote against former Air Force pilot Shak Hill. She risked losing Republican voters by breaking with President Trump on health-care legislation and his desire for a government shutdown.

“Congresswoman Comstock gets results for the constituents she serves . . . . Barbara Comstock has beaten the DCCC picked candidates time and again,” her campaign manager, Susan Falconer, said in a statement.

The diverse, suburban 10th District is one of 23 GOP-held districts that Democrats feel they must win to take control of the House. Virginia Democrats hope for a repeat of the enthusiasm that carried Northam to a decisive win in 2017 and nearly flipped the state House of Delegates.

Anti-Trump voters in the district, which stretches from Loudoun and parts of Fairfax and Prince William counties to Clarke and Frederick counties in the west, unseated nearly every GOP state lawmaker representing the same territory as Comstock.

Wexton’s campaign illustrated the strong link between the nation’s capital and the 10th District with a television ad that showed her riding a box truck, featuring a “Change is Coming” banner, from Virginia to the White House.

Virginia primary election results 2018: Senate and House races

“I don’t believe in some of the radical views that our current president has and our current Republican Party has,” said Elaine Lynch, a 42-year-old graphic designer and Wexton supporter from Great Falls. “To me, this vote is a vote for change.”

She also pointed to a major issue in the primary on which Wexton and Comstock disagree: gun control.

Lynch was disappointed that Comstock, who has an A rating from the National Rifle Association, did not support gun-control measures after mass shootings at Marjory Stoneman Douglas High School in Parkland, Fla., and Santa Fe High School in Texas.

“She is now the typical Republican — follow the party line,” she said. “We can’t keep looking at mass shootings at schools and thinking nothing’s wrong.”

Once reliably Republican, the district’s move to the middle and slightly left over the past decade coincided with an influx of young families, many of whom have children. The district not only supported Northam but also voted for Hillary Clinton over Trump in 2016.

On paper, the district looks like a probable pickup for Democrats, but Comstock is a formidable campaigner and experienced operative with a network dating back to the 1990s, when she investigated the Clintons on Capitol Hill.

Even sharing a ticket with Trump, Comstock managed to outperform him by 16 points.

“She’s sort of a middle-of-the-road type Republican,” said Bruce Goddard, a 70-year-old retiree from Great Falls. “She’s not on the wacky right and she’s not on the wacky left, so I’m happy where she is.”

His wife, Valerie Goddard, 65, said that despite all the noise at the federal level, Comstock deserves a third term because she focuses on issues residents care about, such as veterans benefits.

“I think she’ll be okay,” she said. “She’s a good performer. She’s out there on the issues.”

While Democrats say Comstock is too conservative for the district, supporters of Hill, her GOP challenger, say she is not conservative enough.

In a primary where Democrats mostly agreed on the issues, Wexton avoided moving too far to the left, while a rival, Army veteran Dan Helmer, wanted to impeach Trump, legalize marijuana and institute a Medicare-for-all option.

He raised the ire of the White House with a political ad comparing Trump to Osama bin Laden, and he produced an undercover video in which he legally purchased a semiautomatic assault rifle at a gun show without a background check in minutes.

Friedman, who worked at the State Department on human-trafficking policy during the Obama administration, also posed a threat based on the size of her campaign treasury. After raising $1.4 million, Friedman, the great-granddaughter of the vice president and chair of Levi Strauss & Co., gave her campaign $1 million, allowing her to air TV ads.

Helmer and Friedman criticized Wexton for voting for a bipartisan guns deal in Richmond that expanded concealed-carry rights but also added protections for domestic-violence victims. Friedman reinforced the message with negative mailers.

While briefing volunteers preparing to knock on doors Saturday, a Wexton campaign staffer said the attacks were resonating with voters and then encouraged her supporters to remind voters Wexton has an F rating from the NRA.

Lindsey Davis Stover, a former chief of staff on Capitol Hill who worked in the Obama administration, avoided negative campaigning while focusing on western parts of the district often ignored by Democrats.

Former federal prosecutor Paul Pelletier and scientist Julia Biggins went to nearly every forum but did not have the fundraising muscle to compete across the district.

All candidates acknowledged it will not be easy to beat Comstock despite the national mood, and they got a reminder of that Tuesday.

As Democrats voted, the House passed opioid legislation Comstock co-sponsored with a Democrat: Rep. Ben Ray Luján (D-N.M.), chairman of the Democratic Congressional Campaign Committee.

Rachel Chason contributed to this report.