In the end, those assurances were enough to win over a handful of skeptical senators. Two Republican no votes — and opposition from Senator John McCain of Arizona, the victim of torture in Vietnam who was not present for the vote — were more than offset by six Democrats, most of whom represent states that Mr. Trump won in 2016. Ms. Haspel also won over Senator Mark Warner of Virginia, the top Democrat on the Senate Intelligence Committee, who had led the interrogation of her record.

She was confirmed 54 to 45.

Ms. Haspel is now set to take over a spy agency that has managed to keep a low profile under Mr. Trump in recent months. He was sharply critical of America’s intelligence agencies before taking office, even comparing them to Nazis at one point. But Ms. Haspel’s predecessor, Mike Pompeo, who is now the secretary of state, built a warm rapport with the president.

Inside the agency, Mr. Pompeo had a more mixed reputation. He won praise for promoting agency veterans, including Ms. Haspel, who served as his deputy, and for pushing Mr. Trump to allow the C.I.A. to take on more aggressive covert operations. But Mr. Pompeo’s overt politics — he had been a firebrand Republican House member before taking over the C.I.A. — made many there uneasy that their work could be infected by political concerns.

Ms. Haspel is free of that particular baggage. Her nomination was seen by many at the C.I.A. as the best chance the agency had to avoid having a political partisan brought in as its director. Still, it remains to be seen how Ms. Haspel will get along with Mr. Trump, a president who prizes personal relationships above all else.

As a veteran clandestine officer, she lived and worked in secret and has never played any kind of public role. The C.I.A., typically reticent to draw attention to the work of its operatives, undertook an overt campaign to support Ms. Haspel’s nomination, declassifying aspects of her career to build a positive public image and authorizing a cadre of former officers to speak with reporters. Behind the scenes, former high-level officials from the agency pressed senators to get behind her nomination.