DONETSK, Ukraine — Far from its best, Spain still advanced to another major final, beating Portugal 4-2 in a penalty shootout Wednesday after a scoreless draw at the European Championship.

Cesc Fabregas, who came on as a substitute in the second half of regulation time, scored the deciding penalty after Bruno Alves hit the crossbar for Portugal moments earlier.

“I played poorly, but the team worked really hard,” Fabregas said. “I had this intuition that we could advance if we went to penalties and that’s what we did.”

Spain, seeking its third straight major trophy after claiming titles at Euro 2008 and the 2010 World Cup, will next face either Germany or Italy on Sunday in Kiev.

“Being in another final is a miracle,” Fabregas said. “It’s really incredible.”

Cristiano Ronaldo had several chances for Portugal, but he sent three shots well over the bar as his team held its own for the entire match. The Real Madrid star, who came alive with three goals in his last two matches at the tournament, did not take a penalty in the shootout. He had been slated to take the fifth one, but it never got that far.

“Our players trained and were prepared for a situation like this,” Portugal coach Paulo Bento said. “We didn’t have much luck.”

After an often dour opening 90 minutes in which the Spanish failed to impress, the match livened up in the 30 minutes of extra time.

Spain midfielder Andres Iniesta forced Portugal goalkeeper Rui Patricio into a superb reflex save in the 103rd minute. Iniesta ghosted into the penalty area then held his head in disbelief as Rui Patricio reacted brilliantly to get a strong hand on his shot after Jordi Alba cut the ball back toward the penalty spot.

Rui Patricio made another fine save to deny substitute Jesus Navas in the 111th.

“Both teams were stronger in defense in the first 90 minutes, but that balance was broken in extra time,” Spain coach Vicente del Bosque said.

Tempers frayed at times, with Portugal center half Pepe getting a yellow card for a challenge on Xabi Alonso as they went for a high ball. Ronaldo also received some heavy challenges late in the match.

In the shootout, Xabi had the first attempt saved by Rui Patricio before Spain goalkeeper Iker Casillas saved Joao Moutinho’s shot. Iniesta, Gerard Pique and Sergio Ramos then all scored for Spain, while Pepe and Nani answered for Portugal.

“The first one wasn’t so lucky, and then we scored the rest of them,” Casillas said. “Yes, we really were lucky. Everything is about luck sometimes.”

Del Bosque ditched his unorthodox 4-6-0 formation for Wednesday’s match and opted for a traditional striker — but not the one many expected. Instead of Fernando Torres, it was Alvaro Negredo that got the start up front. But 10 minutes into the second half, Del Bosque replaced him with Fabregas.

Although Negredo worked hard, his lack of movement made him easy to mark. Spain improved as Fabregas made clever runs to stretch the defense and the defending champions dominated most of extra time with the substitution of Pedro Rodriguez for midfielder Xavi Hernandez late in the match.

“I think we were better in the first 90 minutes. Then in extra time we were less efficient,” Bento said. “When we couldn’t take advantage of our opportunities, Spain got stronger.”

Rui Patricio was a big help in goal for Portugal, but much more was expected from Ronaldo. The winger had a chance, albeit a difficult one, to win the game in the 90th minute when Portugal poured forward after clearing a free kick. But Raul Meireles’ pass was fractionally behind Ronaldo and, with momentum lost, he had to check his run and sent his shot high and wide.

Ronaldo briefly threatened early in the first half, whipping over a cross from the left wing that Casillas grabbed before Nani could head it in. He also sent a free kick into the wall after too easily tumbling under a challenge.

Working as a unit, Portugal started to gain the upper hand midway through the first half. Moutinho flicked the ball into the path of Ronaldo, whose wild shot flew over the net.