Here's a solution which works, comment down below if you want to know something more on this!
public class GoogleSearch {
public static void main(String[] args) {
WebDriver driver=new ChromeDriver();
driver.get("http://www.google.com");
/*get method of WebDriver class will navigate you to the specified site*/
driver.findElement(By.xpath("//input[@type='text']")).sendKeys("Software Testing");
/*findElement of the WebDriver class will be to used to locate the webelement(here, Search box ) by xpath(located from chropath by inspecting), and the sendKeys method for typing the mentioned text in the search box.*/
driver.findElement(By.xpath("//button[@name='btnG']")).click();
/* findElement method of WebDriver class to locate the Submit button element by xpath and click method to click that button.*/
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
/*This specifies the amount of time the driver should wait when searching for an element if it is not immediately present.*/
driver.findElement(By.xpath("(//h3[@class='r']/a)[3]")).click();
/* thid parameter in the xpathmethod will choose the 3rd option from the search menu.*/
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
/*This specifies the amount of time the driver should wait when searching for an element(here the 3rd search for to get loaded and displayed) if it is not immediately present.*/
}
}
Comments with respect to the commands are written below to it.
If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s Selenium certification!