Selenium Commands with Examples - SELENIUM

A command is what tells Selenium what to do.

Element Locator

Selenium uses what is called locators to find and match the elements of your page that it needs to interact with.
Element Locators tell Selenium, which HTML element a command refers to. Selenium webdriver generally uses 8 locators to find the elements on a web page. The following are the list of some object identifier or locators supported by selenium. 
The format of a locator is: locatorType=argument
Example: – (“link=click”)
Selenium supports the following strategies for locating elements:
Select the element with the specified @id attribute
Select the first element with the specified @name attribute.      
• username
• name=username
Locate an element using an XPath expression.
• xpath=//img[@alt=’The image alt text’]
• xpath=//table[@id=’table1′]//tr[4]/td[2]
Select the link (anchor) element which contains text matching the specified pattern.
• link=The link text

Related Page: How To Use WebDriver Backed Selenium

Important Selenium Commands

Accelerate your career with Best Selenium Training and become expertise in Selenium.

Selenium Script Explanation

Here, we can see the code for simple login and logout functionality with comments and the explanation of the code.
Here is the sample script which will open a page…
This script was written using JUnit…
import org.junit.After; //Junit package
import org.junit.Before; //Junit package
import org.junit.Test; //Junit package
import org.openqa.selenium.server.SeleniumServer; //Selenium server package
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.SeleneseTestCase;
import com.thoughtworks.selenium.Selenium;
public class Google1 extends SeleneseTestCase { //Class name[class name should be same as the program name]
private Selenium selenium;
private SeleniumServer seleniumServer;
public void setUp() throws Exception { //Set UP method to start selenium and selenium server
/*Defining the selenium method.
DefaultSelenium selenium=new
4444 — serverport
Iehta(IE) or iexplore(IE) or firefox—– browser start command(we can use *firefox also)
http:// —browser URL(Here we can give full URL of site) */
selenium = new DefaultSelenium(“localhost”, 4444, “*iehta”, “http://”);
seleniumServer = newSeleniumServer();
@TEST —//Our actual test case steps come here
public void testText1() throws Exception {“”);
@After // To stop selenium and selenium server
public void tearDown() throws Exception {

Related Page: How To Locate & Find Elements - SELENIUM


Basically JUnit is an open source unit testing tool and used to test small/large units of code. To run the JUnit test you don’t have to create class object or define main method. JUnit provide assertion library which is used to evaluate the test result. Annotations of JUnit are used to run the test method. JUnit is also used to run the Automation suite having multiple test cases.
JUnit is otherwise, a unit testing framework which mostly used for unit testing of Java projects. JUnit assumes that all test methods can be performed in an arbitrary order. Therefore, tests should not depend on other tests.
To write a test with JUnit

Checkout Selenium Interview Questions


Mark your test cases with @TEST annotations. You don’t need to prefix your test cases with “test”. In addition, your class does not need to extend from
“TestCase” class.
public void addition() {
assertEquals(12, simpleMath.add(7, 5));

@Before and @After
Use @Before and @After annotations for “setup” and “tearDown” methods respectively. They run before and after every test case.
public void runBeforeEveryTest() {
simpleMath = new SimpleMath();
public static void runAfterClass() {
// run for one time after all test cases

Related Page: GoBack, Refresh, MouseOver And Highlight Commands - SELENIUM

Selenium Commands

Text box:
selenium.type(“locator”, “text”);

Sets the value of an input field, as though you typed it in.
locator an element locator
value the value to type
Link/button/Check box/Radio button:“link=linktext”);

Clicks on a link, button, checkbox or radio button. If the click action causes a new page to load (like a link usually does), call waitForPageToLoad.
an element locator

Drop Down Box:

a) Select value:“locaotr”, “lable=value”);
Select an option from a drop-down using an option locator.
an element locator identifying a drop-down menu
optionLocator an option locator (a label by default)
b) Display all values:
Gets all option labels in the specified select drop-down.
an element locator identifying a drop-down menu
an array of all option labels in the specified select drop-down
Multiselect ??List box
selenium.addSelection(locator, optionLocator);
Add a selection to the set of selected options in a multi-select element using an option locator.
an element locator identifying a multi-select box
optionLocator an option locator (a label by default)
selenium.removeSelection(locator, optionLocator);
Remove a selection from the set of selected options in a multi-select element using an option locator.
an element locator identifying a multi-select box
optionLocator an option locator (a label by default)
Capture screen shot(positive):

Captures a PNG screenshot to the specified file.
the absolute path to the file to be written, e.g. “c:blahscreenshot.png”
selenium.captureEntirePageScreenshot(filename, kwargs);
Saves the entire contents of the current window canvas to a PNG file. Parameters:
filename the path to the file to persist the screenshot as. No filename extension will be appended by default. Directories will not be created if they do not exist, and an exception will be thrown, possibly by native code.
kwargs a kwargs string that modifies the way the screenshot is captured. Example: “background=#CCFFDD” . Currently valid options:
background the background CSS for the HTML document. This may be useful to set for capturing screenshots of lessthan- ideal layouts, for example where absolute positioning causes the calculation of the canvas dimension to fail and a black background is exposed (possibly obscuring black text).
Capture screen shot(with failure)
public Scroll()

Related Page: Learn Java Basics For Selenium

How to use Java script in selenium:

Gets the result of evaluating the specified JavaScript snippet. The snippet may have multiple lines, but only the result of the last line will be returned.
the JavaScript snippet to run
the results of evaluating the snippet
Page scroll down using Java script:

Scrolls down the page 500 pixles.
Normal alerts
Retrieves the message of a JavaScript alert generated during the previous action, or fail if there were no alerts.
The message of the most recent JavaScript alert
On load alerts: This can be handled by using Selenium 2
Handling Multiple windows and popups
Extracting window IDs with Selenium :
Returns the IDs of all windows that the browser knows about.
the IDs of all windows that the browser knows about.
Returns the Namess of all windows that the browser knows about.
the NAMESs of all windows that the browser knows about.
Returns the Titles of all windows that the browser knows about.
the Titles of all windows that the browser knows about.
Tabbed browsing and handling tabs with selenium RC Handling popups/new windows, performing actions in pop up window Closing pop up windows

Related Page: Debugging - SELENIUM

Use Attach Source to see the Selenium Driver Source Code

The first thing we will do is configure Eclipse to let us see the source code for the Selenium Driver.
You should see a Class File Editor window, with the message “Source not found”. This is because we haven’t tol d Eclipse where the source code for the Selenium Java Driver and Server resides on the disk.
We can easily fix this by pressing the [Attach Source. . . ] button and navigating to the source code .jar file
• selenium?java?2.X?srcs.jar

Why should we do this?

With Eclipse setup like this, we can start to understand the code that we are using. It is very important to understand as much as
possible about the libraries and classes that you are using in your testing. You can start to understand the limitations or restrictions
of these classes and learn what you have to build yourself, and what comes for free in the short term to help you get started. If
you are just learning programming then you have a whole set of relevant source code to study to look at how other people have
used and built the tools. Reading source code is one of the easiest ways for a novice programmer to advance their programming skills.

Checkout Selenium Online Tutorial

How to find line numbers in Eclipse?

To help you find the line number you can:

• switch on line numbering by right clicking on the left margin of the code window (where you set breakpoints) and select “Show line numbers”

Explore Selenium Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!



Get Updates on Tech posts, Interview & Certification questions and training schedules