“In Trump’s America, people are getting screwed and those getting screwed more than others are people who’ve never had a voice in the government,” Mr. Keam said. “Those are motivations a white guy wouldn’t have.”

Some are skeptical of reading too much into one off-year election. And even Democrats have had heated disagreements over whether identity politics help the party or drive people away. But David Ramadan, a Republican who served in the Virginia General Assembly from 2012 to 2016 said the warning for his party was clear.

“Tuesday’s results show that unless the Republicans go back to being mainstream conservatives and run on issues like education, jobs and transportations instead of sanctuary cities and Confederate statues, they will hand not only Virginia to liberals, but they will hand the country to liberals and Congress to liberals next year,’’ Mr. Ramadan said.

Danica Roem

Even before her election, Danica Roem drew national attention as a transgender woman running against a Republican who had introduced a “bathroom bill” in the Virginia Legislature to bar transgender people from restrooms.

Ms. Roem, 33, a former reporter for a newspaper in the Washington suburbs, tried to focus on issues like traffic, while fending off attacks from Republicans, including the state party, that she was morally degenerate and not really a woman.