How To Search Using Google and Click on a Specific Link Using Selenium?

Share and Enjoy !

To start with selenium learning, the first step I suggest is to automate google. In this exercise i will try to show how to automate google.

The objective of this post is to automate a flow where steps are–

  1. open a firefox browser 
  2. Navigate to google. 
  3. in the search box type askqtp. 
  4. Click on search
  5. list the links available in the search
  6. Click on the first link
  7. Read the title. 

for this

//Normal imports
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.firefox.FirefoxDriver;

public class ClickaLink {

public static void main(String[] args) {
//Creating the web driver
WebDriver fd=new FirefoxDriver();
//Step 1&2
WebElement box=fd.findElement(By.xpath("//*[@id='res']"));
//the center box in the results page that has all the links Step-5
//xpath of the links displayed

System.out.println("Total Links -->" allLinks.size());
for(int i=1;i<allLinks.size();i++ ){
//click on the first link that is displayed

By this code you will be able to click on the firstlink and automate google search. Hope this helps.

Share and Enjoy !

4 thoughts on “How To Search Using Google and Click on a Specific Link Using Selenium?

  1. Thank you Checca. Your appreciation will help us to write more great content. Regards,Animesh

  2. Pingback: Google
  3. Pingback: Google

Leave a Reply

Your email address will not be published. Required fields are marked *