DERA ISMAIL KHAN, Pakistan — Pakistani lawmakers elected a textiles magnate Tuesday to be the next president of a country plagued by Islamic extremism, only hours after Taliban militants launched a mass prison break freeing hundreds of inmates.

The attack highlighted one of the major challenges that Mamnoon Hussain will face once he takes over the largely ceremonial post of president. Security forces appeared totally unprepared for the raid in the northwest, despite senior prison officials having received intelligence indicating an attack was likely.

It was one of the worst Taliban attacks in recent months and raises serious questions about the state's capacity to battle a domestic insurgency that has raged for years and killed tens of thousands of security personnel and civilians.

Pakistan's president is not elected by popular vote, but by lawmakers in the Senate, National Assembly and the assemblies of the four provinces. Many predicted Tuesday's outcome because the ruling Pakistan Muslim League-N party, which nominated Hussain, won majorities in the National Assembly and the assembly of Pakistan's most populous province, Punjab, in June.

Hussain received 432 votes from lawmakers, said the head of Pakistan's election commission, Fakhruddin Ibrahim. The only other candidate, retired judge Wajihuddin Ahmed, received 77 votes. Ahmed was nominated by Pakistan Tehreek-e-Insaf, a party led by former cricket star Imran Khan.

Controversy surrounded the vote. The country's former ruling party, the Pakistan People's Party, which has the second highest number of seats in the National Assembly, boycotted the election over the Supreme Court's decision to move the vote forward to Tuesday. It had been scheduled for Aug. 6, but lawmakers asked for it to be pushed to Tuesday so they could travel to Saudi Arabia toward the end of the Islamic holy month of Ramadan.

Hussain was born to an industrialist family in 1940 in the Indian city of Agra. His family settled in Karachi, the capital of Sindh province, after Pakistan was carved out of British India in 1947, and set up a textile business there. Hussain is a longtime member of the PML-N and served as governor of Sindh for about four months in 1999.

Hussain, who is not a prominent figure in national politics in Pakistan, replaces current president Asif Ali Zardari, whose five-year term ends on Sept. 8. Zardari rose to power after his wife, former Prime Minister Benazir Bhutto, was killed in a gun and bomb attack in December 2007.

Zardari has been a contentious figure as president and often battled with both the powerful army and the Supreme Court.

His biggest accomplishment is seen as guiding Pakistan's first civilian government to finish its full five-year term and transferring power in democratic elections in a country plagued by military coups.

Zardari also agreed to a constitutional amendment that transferred many of the president's powers to the prime minister, leaving his position as largely ceremonial. That leaves Prime Minister Nawaz Sharif as the most powerful figure in the civilian government in Pakistan, a key ally for the United States in battling Islamic militants and negotiating an end to the war in neighboring Afghanistan.

But Zardari's government is widely perceived to have done little to address the major problems facing the country, especially the pervasive electricity shortages that crippled Pakistan's economy and left some people without power for up to 20 hours per day.

"Zardari will be remembered as a quite a controversial president, but a major survivor," said Pakistani analyst Hasan Askari Rizvi.

The army launched major operations against the Pakistani Taliban during Zardari's tenure, but the group has proved resilient and continues to stage frequent attacks against security personnel and civilians.

On Monday night, some 150 Taliban militants armed with gun, grenades and suicide vests stormed a prison in the town of Dera Ismail Khan. The raid bore the hallmarks of a long preparation, including some militants wearing police uniforms and others using megaphones to call out the names of specific prisoners they sought, witnesses and authorities said Tuesday.

The militants, who shouted "God is great" and "Long live the Taliban," killed six policemen, six Shiite Muslim prisoners — one of whom was beheaded — and two civilians, said Mushtaq Jadoon, Dera Ismail Khan's commissioner. Many hard-line Pakistani militants consider the country's Shiite minority to be heretics.

Dera Ismail Khan is located near Pakistan's tribal region, the main sanctuary for Taliban and al-Qaida militants in the country, and many may have fled there. A Taliban spokesman later claimed responsibility for the attack and said some 300 prisoners escaped. Jadoon said 253 prisoners, including 25 "dangerous terrorists," escaped during the assault.

Yet it appears authorities had information the attack was coming. Khalid Abbas, a policeman who heads the prison department in Khyber Pakhtunkhwa, said officials recently received intelligence indicating a possible attack, but they didn't expect it so soon.

Pervaiz Khattak, the chief minister of Khyber Pakhtunkhwa, said nobody informed him about a possible attack. He called it an "intelligence failure" and said he didn't understand how so many heavily armed militants could pass through so many security checkpoints.

"Just a day before, I was given a report of all is good about prison security," Khattak said. "Heads will roll. No one will be spared."