<p>How does selenium work? Can you explain the internal working of it.</p>

First there's a layer of javascript code that is used to automate the browser and simulate events, run and verify tests. Next, you run a proxy server - which you point your browser to - that injects this javascript code. Then, you can talk to this proxy server through another port using a set of commands which causes the proxy server to inject javascript code to be run on(or remote controlling) the running browser. Using this framework you can write automated test scripts in a style very much like writing macros for the browser.