Webdriver Script015 : Handling Ajax Elements

/*********************************************************************

Script Summary

Title  : Webdriver Script015 : Handling Ajax Elements
Author : Gaurav Khanna

1. Set the Base URL to "http://www.google.com".
2. Type word 'Testing'
3. Pause for 5 seconds.
4. Print list of names suggested in Ajax Auto Suggested List

 *********************************************************************/

package com.gaurav.webdriver;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class s015_HandlingAjaxElements {

    // Declaring variable 'webDriver' of WebDriver Type
    WebDriver webDriver;

    // Declaring baseURL variable of String Type
    String baseUrl;

    @BeforeMethod
    public void startUp() {

        // Initializing FireFox Driver
        webDriver = new FirefoxDriver();

        // Assigning URL to variable 'baseUrl'
        baseUrl = "http://www.google.com";
    }

    @Test
    public void tests015_HandlingAjaxElements() throws InterruptedException {

        //
        webDriver.get(baseUrl);

        //
        webDriver.manage().window().maximize();

        //
        webDriver.findElement(By.id("gbqfq")).sendKeys("testing");

        //
        Thread.sleep(5000L);

        int i = 1;

        try {
            while (true) {
                String val = webDriver
                        .findElement(
                                By.xpath("//html/body/table/tbody/tr/td[2]/table/tbody/tr["
                                        + i + "]/td/div/table/tbody/tr/td/span"))
                        .getText();
                System.out.println(val);
                i++;
            }
        } catch (Exception e) {
            System.out.println("Exception at Element No. " + i);
        }
    }

    @AfterMethod
    public void shutDown() {

        // This will close the browser
        webDriver.quit();
    }

}