Abstract-

In Previous post, we have learnt how to launch a web-browser in selenium. In this post we will learn how to enter values in a text box and how to click a button.

We will be using chrome browser for this posts well.

package TestNG_Classes;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Selenium_FirstStep {

/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
WebDriver driver ;

// TODO Auto-generated method stub E:\chromedriver_win32
String exePath = "E:\\chromedriver_win32\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", exePath);

driver=new ChromeDriver();

driver.manage().window().maximize();

driver.get("http://www.google.com");

//Entering values in google search text box, Value entered is Automatorsworld
driver.findElement(By.id("lst-ib")).sendKeys("Automatorsworld");

// Clicking search button
driver.findElement(By.name("btnK")).click();

}

In above code there are 2 values which are –

1- By.id(“lst-ib”)

2- By.name(“btnK”)

They are read from web page, In below section, we will see how to check these values from web page.

First of all these are web-object properties.

A web object is identified by its properties, these properties should be used in below recommended sequence-

i- ID – Easy to read, refer below screenshot
ii- Name – Easy to read, refer below screenshot
iii-Xpath – I will posta separately in another post to calculate xpath
iv- CssSelector- I will post a separately in another post to calculate CssSelector

Many times ID and name will not be available in such cases xpath and CssSelector needs to be used.

CssSelector is preferred over xpath as same xpath created issues in different browser where as CssSelector work nicely across different browsers.

Refer below screenshot on how identified web object properties of google page text box and button.

1- Right click on the web element for which you want to read their properties.
2- Select Inspect element option which will be the last option.

Property of Text Box-

Property of Google search button-

Couple of questions and answers

What does findelement return and what all selenium api method can be performed
Answer- It find and return webobject. click, sendkeys etc are the methods can be called.

Can same script work on different browser
Answer- Yes, mostly it work on selenium supported webbrowser however sometimes xpath doest work on Internet explorer.

Which all browser selenium supported
Answer-It supports following browser, below is as per seleniumhq.org-
1- Google Chrome
2- Internet Explorer 7, 8, 9, 10, and 11 on appropriate combinations of Vista, Windows 7, Windows 8, and Windows 8.1. As of April 15 2014, IE 6 is no longer supported. The driver supports running 32-bit and 64-bit versions of the browser where applicable
3- Firefox: latest ESR, previous ESR, current release, one previous release
4- Safari
5- Opera
6- HtmlUnit
7- phantomjs
8- Android (with Selendroid or appium)
9- iOS (with ios-driver or appium)

Summary-

Summarising above we have done 3 activities –

1-Launched chrome browser and passed url

2-Entered value in text box

3-Clicked button.

Above 3 activities are done using driver objects which called differnt methods like, get(), findelement().

There are many other selenium API’s which will be using in coming posts.