Step by step selenium – step 2 -How to enter value in text box and how to click a button.

Selenium Basic

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.

Its Prashant Chaudhary, I have 10+ years of experience in software test automation, performance & process automation with various commercial and open source tools. This blog shares my experience on the topic of Automation. The content published on my blog is solely mine, it does not express the views of any of my employers (current or previous).
No Comments

Leave a Reply

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

Selenium Basic
2
Step by step selenium – step 4, Finding X-path…1/2

In Selenium test automation, if webobject properties does not have id, name, class then we left with 2 options- Option 1 calculate xpath Option 2 Calculate XssSelector In this post, we will calculate about the xpath and various XPath expression for webobjects whose attributed are changing dynamically. There are 2 …

Selenium Basic
1
Step by step selenium – step 3, what is findelement method

Abstract- So far, we have learned how to launch a web browser, entering values in a text box and clicking button. In this post, we will learn about findelement method. Test automation is all about get and set, which means get from web and set into web. Talking more specifically- …

Selenium Basic
Step by step selenium – step 1, How to launch a web browser

Introduction – In this blog post, we will see how to run first test in selenium and what are necessary configuration required for invoking browser. There are 2 basic configuration to invoke a browser. First step, download and configure selenium standalone jar. Visit -https://www.seleniumhq.org/download/ , Refer below screenshot. Config the …

Shares