Kathleen Williams works at her home in Bozeman, Mont. A former state lawmaker, Williams won the Democratic primary election Tuesday in the race for Montana's House seat. (Louise Johns for The Washington Post)

Kathleen Williams wanted to be the first woman elected to Congress from Montana since the 1940s, but many people thought she couldn’t even get the Democratic nomination.

She entered Tuesday’s primary late. She raised far less money than two men competing against her.

She got around those problems with a 1,100-mile trek through the state, addressing gatherings sometimes as tiny as two voters. She amplified each road stop through Instagram, Facebook and Twitter — the social media sites that have proved particularly helpful to lesser-known female candidates this year.

Her supporters — “Team Kathleen” skewed noticeably female — knocked on doors and spread her message to their real and online communities. Both of her TV ads stressed the top topic for Democratic voters this year — health care. One talked in personal terms of how she cared for her mother, who has Alzheimer’s disease.

Defying the odds, Williams won the nomination Tuesday by less than 2 percentage points, yet another woman in yet another state to lead a come-from-behind campaign in a Democratic primary this year. Many of these candidates lacked funding, political experience or official party backing, but they were running in a year when women embody the change many voters want.

Peter Quentin Brown, 61, a contractor and residential designer in Bozeman, Mont., said he liked what he heard and voted for Williams, appreciating her “straight talk” and experience as a three-term state legislator. It was a bonus, he said, that she was a woman.

“I think we need a little less testosterone in public office,” said Brown, a political independent. “We need more women in public office, and there does seem to be a groundswell of change in the air in that direction.”

Many see Williams, 57, as the underdog again in the general election, where she will face incumbent Republican Greg Gianforte for the at-large seat. Last year, the successful businessman filled the seat vacated by Ryan Zinke when President Trump tapped him to become interior secretary. Gianforte’s election was notable for what came just before it: The candidate body-slammed a reporter, leading to a guilty plea on an assault charge. A judge later sentenced him to community service and anger-management classes.

Williams is among a surge of women winning Democratic nominations ahead of the November midterm elections that will decide whether Republicans keep control of Congress. In Georgia, Stacey Abrams won the primary as she seeks to become the first black woman elected governor in U.S. history. In Kentucky, Amy McGrath, a pioneering fighter pilot, overcame a 47-point disadvantage in early polling to win the primary without Democratic Party backing.

David Wasserman, who analyzes House races for the nonpartisan Cook Political Report, said that of the 84 Democratic primary contests this year in which there was no incumbent and at least one woman and one man were on the ballot, women have been the top vote-getters in 59 races.

“Democratic primary voters are saying the best way to send a message to Trump is to nominate a woman,” he said.

One of the elements propelling women is activism that exploded after Trump’s election, reflected in the new volunteers flooding the campaigns of women. The volunteers and social media have, for Williams and other candidates, compensated for difficulty in fundraising that has often confounded women running for office.

Targeted online ads and videos are not only effective but far cheaper than traditional TV spots and can be spread free by supporters. Social media can boost name recognition fast.

“It’s a pretty scary world when people who would be excellent don’t want to run for office because they don’t want to raise $1 million,” Williams said. She spent less than $300,000 in her successful bid, a third of the spending of her closest opponent, John Heenan.

“The lesson to me is that you need enough money to get your message out, but the most money does not equal a win,” Heenan said.

He said that even at his own spaghetti dinners, he could hear that Williams’s message was resonating. “I like you and Kathleen,” they told him.

Heenan, a Democratic lawyer and business executive, said Williams represents a strong contrast to Gianforte, a wealthy business owner. He said that she stands out as “someone who listens, who engages,” and that it bodes well for her chances in November that in the current climate, “you are never going to win by having more TV ads.”

Williams talks more about her experience — multiple terms in the state legislature — than her gender. But at least one of her mailings featured a photo of a group of women standing in her kitchen, an image that underscored the gender factor in her race.

“I had a lot of women say they were not sure a woman could win in Montana, and after a while I got blunt: ‘Well, it won’t happen if that’s what we think!’ ” she said.

Each of the women running this year has faced different circumstances and exploited different strengths. But there remain striking similarities in their campaigns.

Like Williams, Kentucky’s McGrath, a mother of three and a retired Marine combat pilot, also entered her congressional race fighting against the view that she had little chance of winning. For starters, the Democratic Party’s support went to Jim Gray, the popular Lexington mayor.

That official nod is seen as a signal of a candidate’s viability and smooths the path to donors. But McGrath raised $350,000 in donations from around the country in just 72 hours after a powerful video of her pioneering career went viral.

“It’s definitely harder without party support,” said McGrath, reached by phone at Walt Disney World in Orlando with her children. But that came with a bonus: She was freed from the party’s requirements about how to spend her money. She opened six field offices, put up billboards, wrote op-eds and ended up winning in 18 of the 19 counties.

She estimated that 75 percent of her volunteers are women.

“It’s a function of the age of Trump,” McGrath said. She said one of the many powerful motivators for women to get involved in politics is the photo millions saw of “Trump surrounded by a bunch of older men in a room talking about women’s reproductive health.”

In Montana, a Williams victory in November would mark the first time a Democrat has held the state’s sole seat in the House in three decades.

But the race is expected to be fierce. Gianforte, who founded a software company, is one of the richest members of Congress and is expected to run a formidable campaign in a state that Trump easily won.

A key contrast between the two is gun rights. In the red state, Williams challenged the National Rifle Association, proposing limits on sales of assault-style rifles and expanded background checks for gun buyers.

“If the gun lobby wants to give me an ‘F’ for talking about common sense, I’ll proudly say that ‘F’ stands for fearless,” Williams said.

Gianforte benefited in his 2017 special election from more than $344,000 in independent expenditures by the NRA, as well as almost $56,000 from other gun groups.

“We don’t need another extreme liberal in Congress who wants to take our guns away from us,” said Debra Lamm, chair of the Montana Republican Party. “We are well-served by Congressman Gianforte — he is protecting our Montana values, like protecting our Second Amendment rights and supporting President Trump’s tax reform so Montanans have more money in their pockets.”

Gianforte’s campaign did not respond to requests for comment.

Williams says that from school shootings to the tone of public discourse, people want change. She lamented that parents do not want their kids to watch the news.

“We have to cut through the politics and get to the humanity,” she said, adding that voters are thinking, “Holy goodness. Where has our democracy gone?”

Ceilon Aspensen, 54, who lives in Shelby, a rural town in northern Montana, said she is still reeling from Trump’s election. She is a Democrat and said that Williams got her vote Tuesday because she is qualified, cares about protecting the outdoors and took the time to trek across the state in a camper, listening to people.

Besides, she said, “I just decided I was going to try to vote for a woman anytime the opportunity presented itself.”

Sarah Keller in Bozeman, Mont., contributed to this report.