Friday, 13 February 2015

TASK 2


 SEARCHING A STRING IN GOOGLE

      For searching a string, first we have to enter the string to the search field then click the “Search” button to perform the search. Now we are going to automate this process.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Example
{
    public static void main(String[] args)
        {
            WebDriver driver = new FirefoxDriver ();
            driver.get("http://www.google.com");
            WebElement element = driver.findElement(By.name("q"));
            element.sendKeys("Car");
            element.submit();
            driver.quit();
        }
}
Description
     Consider the above code. We know how to load webpage and now we are going to search a string in google. Here we are using the concept of CSS selector. The search input field has an id, name and class associated with it. We are using this attribute to find the input field.

            driver.findElement(By.name("q"));

      Above line of code is used to find the element. The code finds element that have name as “q”. element.sendKeys("Car");” this code actually types the keyword to the search field. element.submit(); this performs clicking function. driver.quit (); this is close the browsers.

No comments:

Post a Comment