Sen. Claire McCaskill of Missouri has survived the growing Republicanism of her state by being good at politics. But she’s also been a bit lucky. And that combination may save her again this November.

McCaskill is not just politically endangered because she, along with nine other Senate Democrats, is running in a red state. President Trump won Missouri by 19 percentage points. But lucky for McCaskill, 2018 is not 2016. Trump is unpopular nationally — and his numbers have dipped in Missouri too. According to Morning Consult, which regularly assesses the president’s standing in each state, Trump had a net favorability rating of +19 in Missouri in January 2017. But in Morning Consult’s most recent state-by-state results, from April, Trump’s net rating was down to +4. And a poll conducted by Emerson College in Missouri late last month found Trump at only +2.

That same Emerson poll showed McCaskill tied at 45 percent with Missouri Attorney General Josh Hawley, who is the favorite to win the Aug. 7 GOP Senate primary in the state. Two other polls from April showed McCaskill with tiny leads (1 and 4 percentage points) over Hawley. So McCaskill is competitive, despite the conservatism of the state. But she is far from a shoo-in. This is likely to be a close race, with both parties spending heavily.

McCaskill has put herself in position to be competitive, despite the conservatism of Missouri. She has voted with Trump’s position on about 46 percent of legislation in the Senate, according to FiveThirtyEight’s Trump score tracker, enabling the senator to position herself as not adamantly opposing everything the president does like some of her more liberal colleagues. At the same time, she voted against the major initiatives that Trump and congressional Republicans pushed last year, the health policy proposals that would have gotten rid of parts of the Affordable Care Act and the tax overhaul. Those votes may not help McCaskill with Trump voters in Missouri, but backing those bills could have hurt her with Democrats, who the veteran senator also needs if she wants to win re-election.

And if you’ve followed McCaskill’s career, it’s not surprising that she is well-positioned in 2018 — she seems to be good at getting elected, staying in office and anticipating where politics is going. In 1992, she was the first woman elected to be the county prosecutor in Jackson County, which includes Kansas City. She was later elected state auditor and just barely lost her 2004 gubernatorial bid. In 2006, she was the first woman ever elected to the U.S. Senate from Missouri, defeating a GOP incumbent. Early in 2008, she endorsed Barack Obama over Hillary Clinton in the Democratic primary, a move that was bold and controversial, since she was the first female senator to embrace Obama over Clinton.

In 2012, McCaskill was in trouble, running for reelection in a year when Obama was on the ballot but unpopular in Missouri. So McCaskill and her campaign ran ads ahead of the GOP primary in Missouri, trying to boost the most conservative candidate in that race, then-GOP Rep. Todd Akin. (One of the ads suggested Akin was “too conservative.” The end of the ad concluded with the senator saying, “I’m Claire McCaskill, and I approve this message.” The goal was to get conservative primary voters to back Akin as a way to annoy Democrats.)

McCaskill felt Akin would be the weakest of the GOP candidates in the general election. It’s not clear how much that ad boosted Akin in the primary, but he won that race and McCaskill easily defeated him in the general election.

So that’s McCaskill being good. Here’s the lucky part.

First, 2006 was a great year for Democratic congressional candidates across the country because of the unpopularity of George W. Bush, the Iraq War and some of the scandals of the Republican-controlled Congress. McCaskill wasn’t on the ballot in 2010 or 2014, years when Republicans were dominant in congressional elections. In 2012, McCaskill may have known Akin was very conservative, but even she could not have predicted his infamous remark, explaining his opposition to exceptions for rape victims in bills that limit abortions, in which Akin said women did not get pregnant from “legitimate rape.” (This comment caused national Republican Party figures to withdraw support for Akin.)

McCaskill has found good fortune again in 2018. Not only is Trump sagging in the polls, but Missouri Gov. Eric Greitens has been indicted on charges of invasion of privacy and computer tampering. Greitens is not on the ballot in 2018, and Hawley has called for the governor to resign, giving him some distance from Greitens. But the controversy has split the state’s GOP between those who support the governor and those who don’t.

Don’t get me wrong: Hawley has a great chance of winning. But if McCaskill pulls off another victory, it would be quite a feat. She would have won three straight Senate elections in Missouri, while the state went from one where Democrats lost at the presidential level by 7 percentage points (2004) to almost 20 points (2016).