How to Automate Google Search with Selenium WebDriver

In this post, you will learn to automate Google Search using Selenium Webdriver with Java. This is a real-time example of Explicit wait command. Google search is an Ajax call. We will handle this Ajax call using the Explicit wait command of Selenium Webdriver. We can also say that we'll automate the auto-suggestion feature of search in this post.

1. What is Ajax search?

Ajax stands for Asynchronous JavaScript and XML. Ajax is a new technology for creating faster and more interactive web applications. What Ajax does is:
  • Update a web page without reloading/refreshing it
  • Read data from the server after the page is loaded
When you write something into a Search Box say "app" and it shows you similar search options that are Ajax. And you can automate that auto-suggestion box with the help of Explicit command in Selenium WebDriver.

Google search is the best example of Ajax. In the case of Google search, you simply type any keyword in the search bar and just below the search bar a suggestion box with matching suggestions appears instantly, this is Ajax. 

We can handle Ajax in Selenium WebDriver with the use of the Explicit Wait command. I have shared below the code for automating Google search. But I recommend executing the below test case manually first and then automating it for better understanding.

2. Example exercise to Automate Google Search with Selenium

Description - Get all the options displayed in the Google auto-suggestion box and print them.

Test Steps:
  1. Launch the Firefox.
  2. Open URL -
  3. Enter the keyword "selenium tutorial techlistic" in the search bar
  4. Wait for the Ajax suggestion box to appear
  5. Get/store all the options of a suggestion box in a list
  6. Print all the suggestions one by one.
  7. You can also click on any of the suggestions using the click() command.
Solution Code:

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

* Test to automate:
* Launch the Firefox.
* Opens
* Type "selenium tutorials techlistic" in search bar
* Wait for ajax suggestion box to appear
* Print all the suggestions one by one

public class AjaxGoogleSearch {

	public static void main() {

	// Set Driver path
	System.setProperty("", "C:\\AUTOMATION\\chromedriver.exe");
	WebDriver driver=new ChromeDriver();

	//open google
	driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

	//enter techlistic tutorials in search box
	driver.findElement("q")).sendKeys("selenium tutorial techlistic");

	//wait for suggestions
	WebDriverWait wait=new WebDriverWait(driver, 20);

	WebElement list=driver.findElement(By.className("sbtc"));
	List rows=list.findElements(By.tagName("li"));

	for(WebElement elem:rows) {

Code Explanation:
  • Set ChromeDriver executable path.
  • Initialize WebDriver chrome browser instance.
  • Get URL.
  • Set Implicit Wait of 30 secs, it means selenium will throw an exception and the test case would fail if any element won't be visible to selenium within 30 secs.
  • WebDriverWait is Explicit Wait in Selenium. It is used to handle Ajax calls. We also call them dynamic elements.
  • We have been given 20 secs to wait for search options.
  • And we are using the object of WebDriverWait class to call until() method to set a condition for our wait. That's why we also call Explicit Wait Conditional Wait.
  • Inside until(), ExpectedConditions class is used, this class has plenty of conditional methods to be used. We have used presenceOfElementLocated.
  • Now, we created a list where we are storing all the 'li' element values, which means we are storing all the search suggestion values.
  • And in the end, using each loop prints all the elements stored in a list.
Note - Please note that Google continuously changes its page elements so please be aware that the elements used in the above example code might be changed while you are automating Google search. So, please make sure that you are using the correct page elements.

Automate Sign-Up Practice Form << Previous || Next >> Automate Amazon like e-Com Website

Passionately working as an Automation Developer for more than a decade.


  1. They also analyse all web pages one by one to make sure they are working and there are no dead links. After all, dead links are extremely bad to have for a website. edsullivan com

  2. this blogs technicaly very sounded thanks for sharing ITC Share Price

  3. It was a very sensible issue that you have raised through this blog. People usually hesitate to write on such topics but you did actually wonderful work. Keep it up!
    website development company


Post a Comment

Popular posts from this blog

Top 7 Web Development Trends in the Market

14 Best Selenium Practice Exercises for Automation Practice

Automation Practice: Automate Amazon like E-Commerce Website with Selenium

17 Best Demo Websites for Automation Testing Practice

Top 10 Highly Paid Indian CEOs in the USA

Mastering Selenium Practice: Automating Web Tables with Demo Examples

Top 51 Most Important Selenium WebDriver Interview Questions

Automate Features with Selenium WebDriver

Python Behave Tutorial: A Comprehensive Guide to Behavior-Driven Development (BDD)