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 Explicit wait command of selenium Webdriver. We can also say that, we'll automate the auto suggestion feature of search in this post.

What is Ajax search?

Ajax stands for Asynchronous JavaScript and XML. Ajax is a new technology for creating for faster and more interactive web applications. What ajax does is:
  • Update a web page without reloading/refresh 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 is ajax. And you can automate that auto suggestion box with the help of Explicit command in Selenium WebDriver.

Google search is a best example of ajax. In case of google search, you simply type any keyword in 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 Explicit Wait command. I have shared below the code for automating Google search. But I recommend to execute below test case manually first and then automate it for better understanding.

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

Test Steps:
  1. Launch the Firefox.
  2. Open URL - http://www.google.com
  3. Enter keyword "selenium tutorial techlistic" in search bar
  4. Wait for ajax suggestion box to appear
  5. Get/store all the options of 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.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

/**
* Test to automate:
* Launch the Firefox.
* Opens google.com
* 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("webdriver.chrome.driver", "C:\\AUTOMATION\\chromedriver.exe");
	WebDriver driver=new ChromeDriver();

	//open google
	driver.get("https://www.google.com");
	driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

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

	//wait for suggestions
	WebDriverWait wait=new WebDriverWait(driver, 20);
	wait.until(ExpectedConditions.presenceOfElementLocated(By.className("sbtc")));

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

	for(WebElement elem:rows) {
	System.out.println(elem.getText());
	}
	}
}

Code Explanation:
  • Set ChromeDriver executable path.
  • Initialize WebDriver chrome browser instance.
  • Get URL.
  • Set Implicit Wait of 30 secs, it means selenium will throw exception and 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 given 20 secs to wait for search options.
  • And we are using object of WebDriverWait class to call until() method to set a condition for our wait. That's why we also call Explicit Wait as 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, means we are storing all the search suggestion values.
  • And in the end, using for each loop to print all the elements stored in list.
Note - Please note that Google continuously change it's page elements so please be aware that the elements used in 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-Commerce Website with Selenium


Author
Passionately working as an Automation Developer from more than a decade. Let's connect on LinkedIn.

Follow Us

YouTube Channel | Facebook Page | Telegram Channel | Quora Space
Feel free to ask queries or share your thoughts in comments or email us.

Comments

  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

    ReplyDelete
  2. You are sent a letter telling you that you do not have insurance, and your escrow account is then adjusted for the inflated premium of a full coverage policy placed by Balboa’s insurance tracking group, run by Steven Ramsthel, Sr Vice President of Loan Tracking Operations &What Is Cdhp Insurance Plan

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

    ReplyDelete
  4. 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

    ReplyDelete

Post a Comment

Popular posts from this blog

10+ Best Demo Websites for Selenium Automation Testing Practice

Automate Amazon like E-Commerce Website with Selenium WebDriver

Handle Static and Dynamic Web Table in Selenium WebDriver

25+ Most Important Selenium WebDriver Commands Tutorial

50+ Most Important Selenium WebDriver Interview Questions and Answers

Top 7 Web Development Trends in 2022

How to Find Broken Links on Webpage with Selenium WebDriver