How to switch to a new window (new tab) which opens up after you click on a link using Selenium WebDriver?

If you click on a link in a web page, then for changing the WebDriver’s focus/ reference to the new window we need to use the switchTo() command. Look at the below example to switch to a new window:

String handle = driver.getWindowHandle();

//get the name of all the windows that were initiated by the WebDriver
for (String handle : driver.getWindowHandles())