Ejecting is possible. It's now thought that solar systems often start with too many planets and they both spiral inwards and outwards due to interactions with each other and the dust and gas cloud of the forming solar system, and some of them may get ejected. Not that many, much fewer of these "rogue planets" than stars.
As for capture, that's very difficult, for a planet (easy for something smaller like a comet).
The first problem is that there are so few rogue planets, and to be captured they would have to come within the system of planets of another star, and the chance of that is tiny. Even stars rarely get that close to other stars.
Then after that, well the planet comes in with escape velocity, for the solar system, so will just depart from it again after a flyby of the star to change its direction.
You need a mechanism for it to lose that velocity. It could do it by a close flyby of a planet, but that would be an even more of a coincidence. Then also that might well make it only a temporary planet - after one such flyby it could have others which might eject it from the solar system again. Or deflect it to hit the star or one of the planets.
You could do it by friction, capture in gas clouds, lose its escape velocity, perhaps if planets escape stars in stellar nurseries they could be captured by other stars in the same forming cluster? But do it too soon, with still lots of dust and gas, and the planet would just lose so much delta v it would spiral into the star.
If there's a fair bit of gas and dust but it's already breaking up into planets, then you can imagine an incoming rogue planet, at the right angle, getting caught by one of those rings of dust and gas perhaps, and it could only spiral in as far as the inner edge of the ring.
You could also do it with a moon - if the planet has a moon and does a flyby which leads to it losing its moon, then the escaping moon could remove some of its delta v, so it could end up in an orbit it can only escape from by recapturing its moon, make it so the moon reaches escape velocity and it can never recapture it.
So - that's just qualitative arguments to give it a bit of a context. Would be interesting to know if anyone has done a proper study of this.
If it can be done. it must be a very rare event. Perhaps easiest in stellar nurseries because the stars are then close together?