Ofelia claims that she ate the two grapes because she didn't think they would be missed. One can draw certain parallels to the Greek myth of Persephone and the Biblical myth of the Garden of Eden. However, she appeared to be in a sort of trance with the forbidden fruit acting as a sort of a Siren's Song.
Something that may also have been of influence is that Ofelia was sent to bed without dinner the evening before as a punishment for ruining her dress and missing the dinner party. The hunger would have made her more susceptible to the "Siren Song" of the fruits displayed at the banquet. Moreover, given the level of rationing at the time, it may be possible that grapes were a luxury that Ofelia now misses.
Also, the fairies were trying to convince her to open the wrong door with the key. Therefore, she no longer trusted them and didn't believe that anything bad would happen if she ate any of the food. So she brushed the fairies away and ate the grapes.
Furthermore, it could be said that Ofelia and her mother, while both in 'discomfort', were both eating from the 'same table'. One could say that Ofelia's mother was not happy with her relationship with the Captain. Nevertheless, although it harmed her soul, she was still attracted by all the pleasures that came along. Ofelia's eating the fruit can be seen as a metaphor for her mother eating at the 'same table' with someone that, at first, looked harmless; but, on further insight, turned out to be a monster.
Various speculations in this FAQ consider why a real person might act in this way, but the reason may have less to do with characterisation and more with genre convention. Anyone who knows European folktales (fairytales) will be familiar with the story structure being used here in which a hero (often a young princess) must complete a series of tasks (usually three) to win a prize. A familiar element of this is a dire warning to avoid some particular action (most often eating). Invariably the hero fails to heed such warnings, and it can be argued that del Toro is simply following the rules in having Ofelia do the same. However, characterisation in the original folktales is often very shallow, and some may feel that a serious movie with more careful characterisation, also requires more care with elements of this kind. Indeed to have Ofelia eat while standing with back firmly turned to a scary monster (which most real children would watch very carefully) raises the suspicion that plot and character logic may have been abandoned here simply for the sake of setting up the dramatic chase which follows.
In addition to all of the above, consider that the underlying theme of a great portion of the movie concerns obedience: Capitan Vidal is blindly obedient; the doctor specifically accuses him of such after refusing the captain's orders to save the tortured man. By contrast, the heroes of this film are the disobedient ones, and so it is only fitting, albeit quite confusing at first, that Ofelia should eat the grapes, even when clearly told not to. Aside from the fact that her doing so also creates quite a juicy (pun intended) moment of tension for the viewer, who is inwardly shouting "Don't eat the grapes!" (a tension that del Toro sets up very obviously by having Ofelia's back to the monster), it is also by having it so completely and perfectly obvious that she absolutely MUST be obedient that helps to very brilliantly underline this moment of pure disobedience on her part.
Also, it is important to remember the third task, in which not following the rules was the best answer: Ofelia passes the test by sparing her infant brother, at the cost of her own mortal life, though she had promised to obey the faun.