There are some mathematicians, astronomers, and scientists who realized that Uranus does not normally follow its orbit 100%. There are times when it goes out of orbit from time to time. Since they did not understand the orbit of Uranus at all, they started to analyze what could be causing the orbit of Uranus to act that way.
They have predicted that there is another planet after it. They also computed where it is going to be. The mathematical calculations proved to be right because Neptune was discovered after. While it is officially introduced as one of the planets in the solar system by the late 1800s, studies show that it was discovered by Galileo even before that.