Showing posts with label Selenium Basics. Show all posts
Showing posts with label Selenium Basics. Show all posts

How to use chrome headless using selenium

How to use chrome headless using selenium

How to use chrome headless using selenium  

A headless browser is a kind of web browser which has no user interface, In other words,  a browser, that access web pages but doesn’t show them to any human being. They’re actually used to provide the content of web pages to other programs. 

Why is that useful?

A headless browser is a great tool for automated testing and server environments where you don't need a visible UI shell. For example, you may want to run some tests against a real web page, create a PDF of it, or just inspect how the browser renders an URL.

Now Chrome also supports headless feature from version 59 .  

Right now, Selenium opens a full instance of Chrome. In other words, it's an automated solution but not completely headless. However, Selenium could use --headless in the future.

There are few bugs  in new chrome driver so we need to use 2 chromeOptions to use headless Chrome in selenium  

Here is code for chrome headless  in windows system 

 import org.junit.Assert;  
 import org.junit.Test;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 public class HeadlessChrome  
  public void createChromeDriverHeadless() throws IOException  
    ChromeOptions chromeOptions = new ChromeOptions();  
    System.setProperty("", "C:\\Users\\anjan\\Desktop\\cucmber\\chromedriver_win32\\chromedriver.exe");  
    WebDriver Driver = new ChromeDriver(chromeOptions);  
    WebDriverWait waitForUsername = new WebDriverWait(Driver, 5000);  
    WebDriverWait waitForError = new WebDriverWait(Driver, 5000);  
    Assert.assertTrue(Driver.findElement("loginbutton")).getText().contains("Log In"));  

How to Run Webdriver in chrome browser?

how to open chrome in selenium

How to Run Webdriver in chrome browser?

To run the Chrome browser in selenium we need to download the Chrome driver separately from the below link  It will provide you with the latest driver and it’s nothing but an ‘.exe file’.  You just need to download it and unzip the file in a proper location.

I can assume that you already know how to install selenium and setup a selenium project. If you do not know how to then you can refer to my previous post selenium installation process for the purpose of setting up a selenium project.

The next thing you need to do is to set the property for Chrome driver and specify its location via the property ‘’ as stated below.

System.setProperty("", "pathofchromedriver");

For the next part what you need to do is to provide the path of the Chrome driver. In my case, my Chrome driver is located in ‘D:/workspace/TestProject/src/chromedriver.exe’ . Here,yYou need to provide your Chrome driver path at "pathofchromedriver"; and after that, you need to initialize the chormeDriver

WebDriver  driver = new ChromeDriver();

Open the browser using the ‘get’ method


Let’s try to open a Chrome browser using selenium webdriver.
 package com.test;  
 import org.openqa.selenium.WebDriver;  
 public class SeleniumMethod {  
  static WebDriver driver;  
  public static void main(String argc[]){  
  public static void openURL(){  
  System.setProperty("", "D:/workspace/TestProject/src/chromedriver.exe");   
  driver = new ChromeDriver() ;  

What is Xpath , Absolute XPath and Relative XPath?

What is Absolute XPath and Relative XPath

What is XPath ?

XPath (XML Path Language), as defined by the World Wide Web Consortium (W3C), is a query-language for selecting nodes from an XML document. In addition, XPath can be used to compute values (e.g., strings, numbers, or Boolean values) from the content of an XML document. The following statements are important information regarding XPath:

XPath is a syntax for defining parts of an XML document
XPath uses path expressions to navigate in XML documents
XPath contains a library of standard functions
XPath is a major element in XSLT and in XQuery
XPath is a W3C recommendation

How to use Absolute XPath and Relative XPath?

Using Absolute XPath

Using Absolute XPath the user can directly access or find elements from the DOM. The main disadvantage of using Absolute XPath is that, if there are any changes made in the path of the element then that XPath fails to identify that particular element.

Absolute XPath begins with a single forward slash "/", which means you can select the element from the root.

Here is an example of Absolute XPath


Using Relative XPath

Using Relative XPath the user can select an element from anywhere from the DOM…
Relative XPath starts with the double forward slashes ‘//’, which means it can search the element anywhere in the webpage.

Here is an example of Relative XPath 


How to Read Excel Files in Java using Apache POI

Apache POI is one of the trusted java package to handle MS office document. Using POI we can read write data in MS Excel using java, Even we can read write in MS word, power point.

In this post, I am discussing some common activities required to do in real life application for MS excel operations.

Some useful classes which are required to handle excel file:

Apache POI main classes usually start with either HSSF, XSSF or SXSSF.
   1.       HSSF is used for Excel 97-2000 file format. E.g. HSSFWorkbook, HSSFSheet.
   2.       XSSF is used for Excel 2007 OOXML (.xlsx) file format. e.g. XSSFWorkbook, XSSFSheet.  
   3.       SXSSF (since 3.8-beta3) is an API-compatible streaming extension of XSSF to be used when very large spreadsheets have to be produced, and heap space is limited. e.g. SXSSFWorkbook, SXSSFSheet. SXSSF achieves its low memory footprint by limiting access to the rows that are within a sliding window, while XSSF gives access to all rows in the document.

XSSF (XML SpreadSheet Format) – Used to reading and writing Open Office XML (XLSX) format files.   
HSSF (Horrible SpreadSheet Format) – Use to read and write Microsoft Excel (XLS) format files.

How to Read Excel File (XLSX) in Java

Writing a file using POI is very simple and involve following steps:

a.       Opening workbook
b.      Point to the sheet in workbook
c.       Traversing row in sheet
d.      Read cells in sheet

     a.       Opening Workbook:
In this example I am using xlsx format means I am using XSSF class.
File fileName = new File("C://testprokect/dataSheet.xlsx"); 
FileInputStream fis = new FileInputStream(fileName );
// Finds the workbook instance for XLSX file
 XSSFWorkbook myWorkBook = new XSSFWorkbook (fis);
Now myWorkBook contains all sheets which are available for the workbook.
// Return first sheet from the XLSX
     b.      Point to the sheet in workbook

workbook XSSFSheet mySheet = myWorkBook.getSheetAt(0);
Here I am using only fist sheet of the workbook. Now mySheet pointing only 1st sheet of the workbook.

// Get iterator to all the rows in current sheet 
      c.       Traversing row in sheet

Iterator<Row> rowIterator = mySheet.iterator();

     d.      Read cells in sheet

// Traversing over each row of XLSX file
while (rowIterator.hasNext()) {
Row row =; // For each row, iterate through each
// columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell =;
switch (cell.getCellType()) {
System.out.print(cell.getStringCellValue() + "\t");
System.out.print(cell.getNumericCellValue() + "\t");
System.out.print(cell.getBooleanCellValue() + "\t");

How to Identify Web Elements Using Selenium Xpath and name

How to find element by Name in selenium

Elements do not necessarily have ID attributes on all of them. Elements can be have names that we can use to locate them uniquely. 

how to use chrome XPath isnpector
In the  image you will find value of the name attribute  of the google search text box is "q".
name is unique for this page.

Now we will write down java selenium code to uniquely identify the web element

WebDriver driver = new FirefoxDriver();
WebElement we = driver.findElement("q"));

Now we can write down the values in the search text box like below method.


How to find element by Xpath in selenium

Before start this topic we need to know what is xpath and how to build the xpath. Most of the time we are using xpath generator tool like Firebug , chrome developer tool ,but sometime we can not solely depend on those tool . We need to design xpath manually to overcome the complex situation.

If we look the the below link we will see there is a situation where we cant not use xapth generator tool directly because need to to use different function to handle the situation.

Please study xpath tutorial to learn what is xpath and how to build the xpath . I am providing few links which will give you a snapshot of the xpath.


How to use xpath in scrip

Sample code

WebDriver driver = new FirefoxDriver();


Selenium Locators

Very basic question what is the locator in selenium, Locators allows us to find elements on a page that can be used in our test.  Actually we need to uniquely identify one element from HTML page where we can do operation like click, select, sendKeys etc.
Selenium webdriver provide us various mechanism to uniquely identify one element from HTML.

1.  Locate elements by ID
2.  Locate elements by Name
3.  Locate elements by Link
4.  Locate elements by Xpath
5.  Locate elements by Css
6.  Locate elements by DOM
7.  Locate elements by PartialLink

Before starting we should begain by making sure that we have all the relevant application installed. While there are not fullproof , they will give us clue how to construct the locator for our test page.

Firefox browser:

1. Firebug: This is a Firefox addon and it allows developer to find elements on the page.

Firebug is available in firefox addon section

firebug install

2. Firepath :

Firepath is available in Firefox addon section.

IE browser:

1. IE Developer tool: This is in build feature for IE ,we can launch by pressing F12.


1. Google chrome developer tool: Like IE this is also in build tool for chrome. By  pressing F12 we can use this tool. We can generate Xpath ,css path using this tool.

find xpath in chrome

copy xpath in chrome
Xapth or css path generation

Now start with locate element by ID

On web application element should have an ID for all there controls on the page. ID should be the unique for that page.

How to find element by ID in selenium 

selenium locators
In the  image you will find the ID of the google search text box.  id="lst-ib".
ID is unique for this page.

Now we will write down java selenium code to uniquely identify the web element (In selenium all web component is a web element) and we will operate on this web element.

Here we are assuming we using Firefox browser
WebDriver driver = new FirefoxDriver();
WebElement we = driver.findElement("st-ib"));

In my next post I will cover below items

1.  Locate elements by Name
2.  Locate elements by Link
3.  Locate elements by Xpath