Thursday, 3 January 2019

Locators in Selenium | Locating Element in Selenium by Identifier

In Selenium a web element on the page should be uniquely identified by the script to perform some actions on it. Actions like Click, Select, Check, Drag Drop etc,

To Locate Element in Selenium, we can use following locators:
  • Id
  • Name
  • Class Name
  • CSS Selector
  • linkText
  • Xpath
  • tagName
  • partialLinkText

Locate Element by ID

Finding element by ID is the most common and fasted way of locating an element. ID's are mostly unique for each elements. If no element has a matching ID attribute, a NoSuchElementException will be raised.

How to find element by ID

Open Developers tool (F12) > Select an element to inspect on the page, we are inspecting 'email' field by its ID attribute as shown below:


Command in Selenium to find Element by ID

driver.findElement(By.id("login-username"));

Locate Element by Name

Finding element by Name is almost same as finding element by ID. Name of elements are not unique all the time. If more than one elements are present on the page with same name then the first element on the web page is selected. If no element has a matching name attribute, a NoSuchElementException will be thrown.

How to Element by Name

Open Developers tool (F12) > Select an element to inspect on the page, we are inspecting 'email' field by its Name attribute as shown below:


Command in Selenium to find Element by Name

driver.findElement(By.name("username"));

Locate Element by Class Name

Use this whenever you want to find element by class attribute. More than one element can have same Class Name on a Page. Probability of Class Name change is high as UI changes.  If no element has a matching Class Name attribute, a NoSuchElementException will be thrown.

How to find Element by Class Name

Open Developers tool (F12) > Select an element to inspect on the page, we are inspecting 'email' field by its Class Name of element as shown below:


Command in Selenium to find Element by Class Name

driver.findElement(By.className("phone-no"));

Locate Element by LinkText

This selector will locate link with specified text on the page no matter where it’s located.

How to find Element by LinkText

Open Developers tool (F12) > Select an element to inspect on the page, we are inspecting 'Privacy' link by its Text as shown below:


Command in Selenium to find Element by LinkText

driver.findElement(By.linkText("Privacy"));

Locate Element by partial Link Text

This selector will locate link with specified partial text on the page no matter where it’s located.

How to find Element by Partial Link Text

Open Developers tool (F12) > Select an element to inspect on the page, we are inspecting 'Privacy' link by its  Partial Text as shown below:


Command in Selenium to find Element by Partial Link Text

driver.findElement(By.partialLinkText("Priva"));

Locate Element by Xpath

When we don't have specific ID, Name attribute of an element, we can use Xpath to find that element. We can either use Relative Xpath or Absolute Xpath(from the root of HTML) of an element.
Script can fail on using Absolute Xpath as any minor changes in position of elements can break it.

How to find Element by Xpath

Open Developers tool (F12) > Select an element to inspect on the page, we are inspecting 'email' field as shown below: Right Click and Copy Xpath

//*[@id="login-username"]

Command in Selenium to find Element by Xpath

driver.findElement(By.xpath("//*[@id="login-username"]"));

Locate Element by Tag Name

A web page can contain multiple Tag of same type. So either use it when any Tag is specifically used on a web page or to get List of web elements of same type.

How to find Element by Tag Name

Open Developers tool (F12) > Select an element to inspect on the page, we are inspecting 'email' input field by its tag Input as shown below:


Command in Selenium to find Element by Tag Name

driver.findElement(By.tagName("input"));

Locate Element by CSS Selectors

A CSS Selector is a combination of an element selector and a value which identifies the web element within a web page. If no element has a matching CSS Seelctor attribute, a NoSuchElementException will be thrown.

Types of CSS Selectors : 

  • ID
  • Class
  • Attribute
  • Sub String
  • Inner Text

How to find Element by CSS Selector

Open Developers tools(F12) > Select an element to inspect on the page as shown below:
We will use this screenshot to explain all types of CSS selectors

How to find Element by CSS Selector by ID


An element’s id in CSS is defined using: “#” - ID's must be unique within the DOM
Example from above screenshot.

#login-username

How to find Element by CSS Selector by Class


An element’s class in CSS is defined using: “.” 
Example from above screenshot.

.phone-no

How to find Element by CSS Selector by Attribute


We can use an attribute selector in selenium to choose elements based on any attribute value.
Example from above screenshot.

input[name='username']
input[id='login-username']

How to find Element by CSS Selector by Sub String


Selenium allows sub string matches using ^=, $=, or *=

  •  ^= Match a prefix
  •  $= Match a suffix
  •  *= Match a substring
Examples from above screenshot.

input[id^='login-']
input element with an “id” that starts with the text “login-”
input[id$='login-username']
input element with an “id” that ends with the text “-username”
input[id*='login']
input element with an “id” that contains the text “login”

How to find Element by CSS Selector by Inner Text


This selector will find link 'Terms' and 'Privacy' on the web page irrespective of its location.
Example from above screenshot.

 a:contains('Terms')
 a:contains('Privacy')

Command in Selenium to find Element by CSS Selector

driver.findElement(By.cssSelector("#login-username"));

driver.findElement(By.cssSelector(".phone-no"));


Wednesday, 26 December 2018

Jenkins Retry Failed Builds

We will use Jenkins Naginator Plugin to rerun the Failed/Unstable builds. This plugin allows you to automatically reschedule a build after a build failure.

Configure

  • Go to Manage Jenkins > Manage Plugin > Available Tab > Search 'Naginator' and install and restart


  • After installing the plugin, add the Post-Build action "Retry build after failure" on your Jenkins Job configuration page.


  • Set Build Retry

Above set Build retry screenshot shows that:

  • Job is enabled to rerun for unstable as well as Failure builds
  • Delay the build execution by Fixed 60 seconds
  • Maximum number of successive failed builds count is set to 3 (Limits successive failed build retries. Set to 0 for no limit.)

Friday, 3 October 2014

How To Execute JavaScript In Selenium WebDriver

Sometimes it is important to run JavaScript directly from the code to get any property of  Browser-BOM,  HTML-DOM  etc.
  • The Browser Object Model (BOM) allows JavaScript to "talk to" the browser. It includes Window, Navigator, Screen, History, Location etc.
  • With the HTML DOM, JavaScript can access and change all the elements of an HTML document. It includes Document, HTML, CSS, Elements etc.

We can execute JavaScript in 2 simple steps:

  1. Cast the WebDriver instance to a  JavascriptExecutor
    JavascriptExecutor js = (JavascriptExecutor) driver;
  2. Execute JavaScript
    js.executeScript("script for execution")

Example:


Saturday, 27 September 2014

Selenium WebDiver: Handle AJAX calls

The most common problem that we face during automation using Selenium WebDriver is how to handle AJAX calls. We never know when the call is complete and page has been updated. AJAX can be handled using JavaScript/JavascriptExecutor.

  In this post we will see how we can use JavaScript - jQuery.active to get all active AJAX calls and wait until all calls get served.

jQuery.active returns total number of active AJAX connections, and we are waiting until all active calls becomes zero or given Timeout reached.

Hope this helped you!!!

Thursday, 24 July 2014

Selenium WebDriver and TestNG: Running script in different browsers |Invoke different browsers

In this post will cover how to run a selenium script in different browsers.
To do so we will use TestNG and @Parameters annotation. Using TestNG we can directly pass parameter to test methods from testng.xml.  Below example makes it more clear

Prerequisites:


Java

  • Create a java class name BrowserContoller.java in your workspace.
  • Create a method setBrowser(String browser) to your class. This method takes a String as input parameter.
  • Add the annotation @Parameters ("browser") to this method. Value to the parameter will be passed from xml.

TestNG

  • Create a simple tesng.xml name say BrowserControllerTestng.xml
  • Define Test name, parameter and its value (firefox, chrome,  iexplore ) and class name.


Wednesday, 21 May 2014

Selenium WebDriver: Dropdown selection

Selenium has Select class designed to interact with drop down lists. Using it, we can easily choose option by displayed text, index or value.

Example:

Consider a dropdown list as shown below:


Now we just need to pass WebElement into Select Object as shown below

Select dropdown = new Select(driver.findElement(By.id("months")));

To select option say 'February', we can use any one of the method:
  • dropdown.selectByVisibleText("February");
  • dropdown.selectByValue("Feb");
  • dropdown.selectByIndex(2);

I have created a generic class Dropdown with all 3 methods shown above, we just need to call it.

  • Dropdown.selectByVisibleText(driver, By.id("months"), "February");
  • Dropdown.selectByValue(driver, By.id("months"), "Feb");
  • Dropdown.selectByIndex(driver, By.id("months"), 2);




Thursday, 8 May 2014

Selenium WebDriver – Handle Window Focus

Method and Description:

 

Example:

  • Below example opens a page of w3schools.com
  • And then clicks on ‘Try it yourself »’ link, which opens a new window.
  • Switch to new window, perform some actions on it and then switch back to previous window