Oh, that definitely happens. I get like that at work, too.
Try to pay attention to what's different on the days when it's not clicking. Did you get enough sleep the night before? What did you eat or drink earlier that day? etc.
You of course won't be able to do this on test day, but if you're prepping and are realizing you're not mentally there, wander off and do something else. Do a physical activity or something brainless, and eat something healthy (an apple, a tuna sandwich, granola bar) and then try again. If it's still not happening, take a break that day and get something else done. Trying to force it won't help.
My best solution when I'm not mentally there is to eat an apple -- totally wakes up my brain. And watching something silly like Tom & Jerry helps too!!