Conditional statements in Selenium

10 October, 2020


Related Blogs


Conditional statements in Selenium in Detail

Simple if condition:-



{  statement ;

} else { statement


Ex:-     if( int i==0)


s.o.p(“is pass”);


Else {

s.o.p(“is not pass”);


Nested if condition:- It is for multiple conditions.



{  s.o.p (i);


Else if(i==1)




Else if(i==2)




. .

For loop condition:-

For(int i=0;i<10;i++)




Ajax control:-

2Ajax means dynamic when we enter any value in the field based on the input the field will display the list of suggested values, those fields are nothing but ajax control.

Interested in mastering Selenium Training? Enroll now for FREE demo on Selenium Training.

** write a program to type selenium in Google textbox check that selenium historical is displaying in the suggested list or not. If it is displaying then type the value, if not print a message not available.


String splits:-

2the string contains multiple values, if we are planning to check one value in the string, we need to use the split method.

2the string contains multiple one, two, three, four. check that “three” is available in the string. if available print it.


2we can split the string by using some regular expression[value]. The value which we are using to split will be eliminated from the string. The remaining values will be stored in an array.


2It is a class data type where we can store multiple values with similar data types.


String  str[];

Int i[];

Object o[];

String comparison commands:-

2we can compare two strings with the below commands.

1) Equals:-it looks for the exact match, and it is case sensitive

2) Equals ignore case :- It also looks for the exact match, but it is not case sensitive.

String str =” ten,nine,six,seven,one,two,three,four”;

String [ ] s=str.split(“ ,”);


For(int i==0;i<s.length;i++)


If(s[i].executigmore code(“throws”)) }

 Ajax control program:-

Public void f() throws exception {

Driver .get(;

Driver.find element(“gbqfq”)).sendkeys(“selenium”);


String str;

Str=driver.findelement(by.xpath(“// …….”)).gettext();

System .out.println(str);

String  s[ ]=str split(“n”);


For(int i=0;i<s.length;i++)



If(s[i].executeignore(“selenium testorial”))




Driver.find element(“gbqfq”)).sendkeys(s[i]);




Learn more about Selenium Interview Questions in this blog post.


2GO to click on leaving from the field and check that Hyderabad, Bangalore &Chennai are available or not.

** How to handle web tables?[how to take the data from o table]

2we can identify the base by using the location lib XPath or id e.t.c

2After finding the element takes the data[text]by using the command get a text()

2the get text () return type in a string

Information commands:-

1) we can have the information from the application which is available with in the tag àget attributes()

2) get text():-It is to take the text or label from the application where it will be outside of the tag.

3) get option():-we can take all the values from a dropdown.

**write a program to print Google search  Hindi display names and tooltip of google image.

Public class information {

Public web driver;


Public void f() {

Driver .get(;

String str =driver.find element(“gbqfbq”)).getattribute(“area-lable”);

System.out.println(“button display name is :”+str);

String str =driver .findelement(“hindi”)).get text();

System .out.println(“link name is :”+str);

String str=driver.find element(“hpolog”)).get attribute(“title”);

System.out.println(“tool tip is”+str);



Public void verify spicejet tooltip() {


String str=driver.find element(by.xpath(“………..”)).get attribute();



  • Write a program to print all the values from the adult dropdown list and check the weather 5 adult is available in the application or not, if available select it.




Whenever we are planning to take multiple elements from the application we have to use “find elements” .the return type of this command is a list.


It is from the collection in java where it is an advanced version of the array. In array, we can store multiple values where it is dependent to the declaration.

Ex:- string str [ ]=new string [s];

  • In the list, we can store multiple values where it is independent of the declaration.
  • Ex:- list<string> str= new arraylist <string>();

Public void f() {


List<web element>str=new select(driver.find element(“….”).get options();


For(int i==;i<str.size();i++){

//system.out.println(str.get(i).get text());

If(str.get(i).get text().equalsignorecase(“5 adults”))


//system.out.println(str.get(i).get text());

New select (driver.find elements(“…..”))).select by visible text(str.get(i).get text());



Selenium Tutorials


** Go to the register page of in

1)print all dropdown id’s

2)print all textboxes id’s

** write a program to print the text box's, button id’s &dropdown id’s in the login page, register 1&register 2 pages of

Validation commands:-

1) Is displayed():- we can validate whether the element is available on the application or not.

2) Is enable():-we can validate either the element is editable or not[either we can perform any action or not]

3) Is selected():- we can validate either the checkbox or radio button is selected or not.

4) Is element present():- It is from selenium RC it works some as like is displayed() in web driver.

** write a program to click on the radio button one way in the and check that either the application is displaying the return date picture.

2check that either username is available in by using web driver and selenium RC commands. If it is not available then print the failed result in the result file.

Public void verify user name() {


If(driver.find element(“f-id”)).is displayed())



} else { system.out.println(“not available”);


If( element present(“f-id”))



} else {

System.out.println(“not available”);



It works the same as if condition. if the condition will give the result in console only were as the assert will give the result in both console and result file[output file].

Assert.assertResults(driver.find element(“f-id”)).is displayed().true);

Assert.asserResults( elementpresent(“f-id”),true);

For an Indepth knowledge on Selenium, click on below

About Author


Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills .