If Gollum was able to see Frodo and attack him when he put on the Ring, why couldn't he see Bilbo in the caverns?
Several reasons. First, Gollum attacked Frodo quickly, before he had a chance to move. Second, Frodo was focused on claiming the Ring, not on dodging the unexpected, and third, Frodo didn't really have a lot of room to maneuver, anyway.
The light sprang up again, and there on the brink of the chasm, at the very Crack of Doom, stood Frodo, black against the glare, tense, erect, but still as if he had been turned to stone.
Frodo is at a cliff-edge. Even if he perceived the danger of Gollum, he could only, really, move away from the edge -- it would be foolhardy to run along the edge. So Gollum could in large part anticipate when Frodo might move.
Then Frodo stirred and spoke with a clear voice, indeed with a voice clearer and more powerful than Sam had ever heard him use, and it rose above the throb and turmoil of Mount Doom, ringing in the roof and walls.
'I have come,' he said. 'But I do not choose now to do what I came to do. I will not do this deed. The Ring is mine!' And suddenly, as he set it on his finger, he vanished from Sam's sight. Sam gasped, but he had no chance to cry out, for at that moment many things happened.
Note that Frodo is speaking with a "clear" and "powerful" voice. He's not ducking, he's not watching for danger -- he's claiming enormous power and mastery in a place of great power. At this moment he feels no fear, no doubt. (But he really should have...)
Something struck Sam violently in the back, his legs were knocked from under him and he was flung aside, striking his head against the stony floor, as a dark shape sprang over him. He lay still and for a moment all went black.
And it all happened very quickly.
Sam ... saw a strange and terrible thing. Gollum on the edge of the abyss was fighting like a mad thing with an unseen foe. To and fro he swayed, now so near the brink that almost he tumbled in, now dragging back, falling to the ground, rising, and falling again. And all the while he hissed but spoke no words.
Gollum rushed Frodo while he was still visible; And though Frodo disappeared before Gollum got to him, Frodo was occupied and trapped; Once Gollum had grappled with him, invisibility was of little value -- wrestlers fight as much by feel as by sight.
While not stated explicitly in the book, the film The Return of the King shows how.
In this clip, at the 2:20 mark, Gollum clocks Sam, and then looks around for Frodo. The shot clearly shows him recognizing that he can see Frodo's footprints, and so he knows exactly where Frodo is standing.
You can see this in the scene Frodo may be invisible however the dust he is kicking up is not, presumably the cavern had a lot less dust or possibly not enough light to see the dust being kicked up.