WebDriver script that opens browser and searches today's weather in google.com

563


In this tutorial, I'll explain how to write a WebDriver script in Java which opens google.com on Chrome and searches for "Today's weather"

package com.selenium.practise;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class TestGoogle {

	public static void main(String[] args) throws InterruptedException {
		
		System.setProperty("webdriver.chrome.driver", "E:\\Soft Wares\\chromedriver.exe");
		// Set path created for ChromeDriver 
		
		WebDriver driver = new ChromeDriver();
		// -> Here we are using ChromeDriver. 
		// -> ChromeDriver() object created and assigned object to WebDriver interface
		
		driver.get("https://www.google.com/");
		// Opening for google application in the Chrome browser.
		
		driver.findElement(By.name("q")).sendKeys("Today's weather");
		//Enter the value(fastfoodcoding) in search box by using name element as "q"
		
		driver.findElement(By.name("btnK")).click();
		//click on the "Google Search" button by using name element as "btnk"
		
		System.out.println("Successfully completed Today's weather search ");
		//print the message in the console window
		
		driver.close();
		//close the window
	}

}