05. Locators

05. Locators

What is Web Element?

  • Web Element represents an?HTML element.
  • Anything which is present on the webpage is called as a web element.
  • HTML documents are made up by?HTML elements. HTML elements are written with a?start?tag, with an?end?tag, with the?content?in between:?<tagname>content?</tagname>
  • The HTML?element?is everything from the start tag to the end tag:

?<p>?my first HTML paragraph.?</p>

  • HTML elements can be nested (elements can contain elements). All HTML documents consist of nested HTML elements.

Locators

  • Selenium uses what is called locators to find and match the elements of the webpage that it needs to interact with the application.
  • Selenium identifies the element using locators.
  • All selenium tool will identify objects or web elements based on the locators, locators are always written by looking at HTML source code

Note: Locators are the lifeblood of the tests. Using the right locator ensures the tests are faster, more reliable or has lower maintenance over releases.?If you’re fortunate enough to be working with unique IDs and Classes, then you’re usually all set. But there will be times when choosing a right locator will become a nightmare. It can be a real challenge to verify that you have the right locators to accomplish what you want.

Types of Locators

There are 8 types of locator are available.

  1. ID
  2. Name
  3. Class Name
  4. Tag Name
  5. Link text
  6. Partial link text
  7. CSS selector
  8. X-Path

  • These locators are categorised into four groups?

  1. Tag name:- tag?
  2. ID, Name and Class Name are called as:- Attribute?
  3. Link text and partial link text:- Text
  4. CSS selector and X-path:- Expression

  • In order to perform operation on the element first we need to search the element by using the ‘FindElement ()’ method of web driver. It takes an argument of type ‘By’.
  • All the above specified locators are available as static method in ‘By class’, all of them takes string as an arguments and returns an object of type ‘By’, which will be passed as argument for ‘FindElement ()’ method.
  • The ‘FindElement ()’ method will search for the required element in the webpage based on the specified locator. If it is found it returns an object of type web element.

Example:

<html>

?????<body>

<a href =?https://www.google/com? id =?a1? name =?n1? class=?c1?>Qspiders</a>

?????</body>

</html>

WebDriver driver = new FirefoxDriver();

driver.get("file://C:/Users/HP/Desktop/mypage.html");

By b = By.id("a1");

WebElement id = driver.findElement(b);

id.click();

  • Instead of writing three statements we always use optimized code as shown below.

Example:?

Using ID:

WebDriver driver = new FirefoxDriver();

driver.get("file://C:/Users/HP/Desktop/mypage.html");

driver.findElement(By.id("a1")).click();

Using Name:

WebDriver driver = new FirefoxDriver();

driver.get("file://C:/Users/HP/Desktop/mypage.html");

driver.findElement(By.name("n1")).click();

Using Link Text:

WebDriver driver = new FirefoxDriver();

driver.get("file://C:/Users/HP/Desktop/mypage.html");

driver.findElement(By.linktext("Qspiders")).click();

Using Partial Link Text:

WebDriver driver = new FirefoxDriver();

driver.get("file://C:/Users/HP/Desktop/mypage.html");

driver.findElement(By.partiallinktext("Qsp")).click();

Using Tag name:

WebDriver driver = new FirefoxDriver();

driver.get("file://C:/Users/HP/Desktop/mypage.html");

driver.findElement(By.tagname("a")).click();

Using Class Name:

WebDriver driver = new FirefoxDriver();

driver.get("file://C:/Users/HP/Desktop/mypage.html");

driver.findElement(By.classname("c1")).click();

Id: - Select element with the specified?@id?attribute.

Name: - Select first element with the specified?@name?attribute.

Link text: - Select link (anchor tag) element which contains text matching the specified link text

Partial Link text: - Select link (anchor tag) element which contains text matching the specified partial link text

Tag Name: - Locate Element using a Tag name.

Class name: - Locate Element using a Tag name.

CSS selector: - Select the element using CSS selectors.?

X-path: - Locate an element using an X-Path expression.

Note:?

  • If duplicate elements are found (more than one element with same locator value) than also selenium performs the operation but on the first element.
  • If there is no element found which matches with the specified locator then it will throw ‘NoSuchElementExecption’.

Error ex:

  • Exception in thread "main" org.openqa.selenium. NoSuchElementException: Unable to Locate element: {"method":"partial link text”, “selector":"spider12"}

CSS Selector:?

  • It is a type of locator and it is an expression which has the following syntax.

Syntax: Html tag [Property name = ‘Property Value’]

  • CSS stands for ‘Cascade Style Sheeting’.
  • Sometime we cannot use the first 6 type of locator(tag Name(), id(),Name (),class Name (),link Text(),partial Link Text()) because it may be not supported or it may not be given by developer or it may be duplicated.

Ex:

<html>

?????<body>

UN: <input type =?text? ><br>

PW: <input type =?Password?>

?????</body>

</html>

  • In the above webpage to identify password filed, we cannot use the linkText() or

partialLinkText() as those not supported for the password filed. We can’t use the

id() , name() , class Name(), as it not given by the developer , we can’t use

tagName() as it is duplicate. In such scenario we can use the

css Selector.

CSS (Cascaded style sheet) with following syntax.

Html tag [property =?value?]

Ex:?

Input [type =’text’]

Input [type =’password’]

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

public class cssselectorexpression?

{

public static void main(String[] args)?

{

WebDriver driver = new FirefoxDriver ();

driver.get ("file:///C:/selenium/HTMLfiles/cssselectorex.html") ;

driver.findElement(By.cssSelector ("input[type='text']")).sendKeys("Pramod");

driver.findElement(By.cssSelector("input[type='password']")).sendKeys("Anagha");

driver.close();

}

}

Note:?

  • We can also use in Double cote in the ?CSS Selector (input[type =\?password\?])? but it we should be escape it using ?\? backward slash.

Ex:?

Input [id=’username’]

Input [class=’text Field’]

Input [name=’username’]

Input [placeholder=’username’]

1) What are the attributes supported by the locator

Ans: -Id (),name(), class().

2) Then how to use other attribute to identify the elements

Ans: We can use CSS Selector () and x path

  • Sometime even css selector also can be duplicate.

Ex:?

<html>

?????????<body>

FN:<input type=?text?><br>

LN:<input type =?text?>

?????????</body>

</html>

  • In the above webpage both first name and last name as same css expression, hence we can’t enter the last name in such cases we use X-path.

Note:

  • In real time application 90% of the web element are never find unique attribute in a backend (id, name) in such cases we have to go for x-path to identify the objects or web elements.
  • Description:

?

Fire Bug:

  • In order to write the code to perform operation on the element we should see the source code of the element, if we right click on the web page and select view page source it will display the complete source code of the webpage. But in that it will be very difficult for a programmer to get the source code of required element. Because of this reason we use a tool called ‘fire-bug’ which is add on for Mozilla Firefox browser.?

What is Firebug?

  • Firebug is a free, open source tool that is available as a Mozilla Firefox extension, and allows debugging, editing, and monitoring of any website's CSS, HTML, DOM, and JavaScript.

Installing the fir-bug:

To install Firebug on Firefox, we will follow these steps:

Step 1: Open Firefox browser and go to?https://addons.mozilla.org.

Step 2: In the search box of the site, type Firebug and hit?Enter?or click on the?Search for add-ons?button.

Step 3: In the search results, click on?Add to Firefox?button.

Step 4: A pop up will appear asking whether we want to continue with the installation. We will now click?Install now.

Step 5: After installation is complete, let's restart Firefox.

  • When the browser comes up, it will prompt us by saying a new add-on has been installed. Now we are all set and ready to play with Firebug.

Note:?

  • Go to the required web page right click on the element and select inspect element with firebug which will display the source code in fire bug window and it also highlights source code of the selected element.
  • Press F12 to see the source code in other browser.

Sample code to login to acti-time:

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

public class ActiTimeLogin?

{

public static void main(String[] args)?

{

WebDriver driver = new FirefoxDriver();

// Go to login page of acti time

driver.get("https://demo.actitime.com/");

// Enter user name

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

// Enter password

driver.findElement(By.name("pwd")).sendKeys("manager");

// click on login button

driver.findElement(By.id("loginButton")).click();

// close the browser

driver.close();

}

}

?

Dynamic element:

  • The element which is changing dynamically during runtime is called as dynamic element.
  • Here, changing refers to change in the position or change in the property values.
  • In order to identify dynamic elements we use x-path.

X-Path:

  • It is a type of locator and it is a path of an element which is present in html tree.
  • It is a way of navigate to entire html document and identify the web element or object using any attribute of the web element/object.?

Note: Originally x-path is the path of an element present in the xml tree.

  • In x-path we will be using following symbols to represent an x-path, they are,
  • While writing the x-path we use ‘/’ followed by the respective html tag, the first ‘/’ indicates beginning of the html tree called root and all the remaining forward slashes are used to access the immediate child element.

//: - go to entire html source code.

/: - go to child.

[]: - represent back end attribute for the web element/ go to parent.

@: - attribute symbol.

*:- match any html tag

.. (Two double dots): - go to parent.?

Note:?

  • To check whether the x-path is correct or not we can use Mozilla fire fox add-on such as fire path (it will be new tab in fire bug itself) or x-path checker.
  • These are used to check whether the x-path is correct or wrong.

?

Steps To Install Fire-Path Add-On In Firefox Browser

Follow the steps given bellow to Install Fire path

  • Open Mozilla Firefox browser.
  • Go to Fire path add on Installation page using URL ->?https://addons.mozilla.org/en-us/firefox/addon/firepath/.?It will take you to fire path Installation page as shown In bellow Image. Click on "Add to Firefox" button as shown In bellow given Image.


  • It will show Software Installation dialog as shown In bellow Image. Click on "Install Now" button. It will start Installing fire path add-on.


  • It will ask to restart Firefox browser at the end of Installation process as shown In bellow Image. Click on "Restart Now" as shown In bellow Image. It will restart Firefox browser.


  • Fire path is Installed now.

How to verify Fire-Path Is Installed properly or not

Follow steps given bellow to verify fire path is Installed properly or not,

  • Click on Firebug Icon from Firefox browser tool bar. It will open firebug window at bottom of the page.

- You will see Fire-Path tab In Firebug window as sown In above Image. That means Fire-path Is Installed properly and you can use it.

X-path is classified into two types

  1. Absolute X-path
  2. Relative X-path

Absolute X-path

  • Whenever the x-path written from root node to child node to identify the objective is called as absolute x-path.
  • Whenever we inspect web element using fire-path, fire-path always generates absolute x-path.
  • Absolute x-path shouldn’t be used in real time selenium script; absolute x-path will fail to identify the object, if whenever required object location changes in the UI.
  • So in real time we should always go for relative x-path.??

Example:?

<html>

?????<body>

<div>

? <input type="text" value="+1"/>

? <input type="text" value="+2"/>

</div>

<div>

? <input type="text" value="+3"/>

? <input type="text" value="+4"/>

</div>

?????</body>

</html>

Html tree structure:

?Html-> body->

  • Div

????-> input[+1]

????->input[+2]

  • Div?

????-> input[+3]

????->input[+4]

  • In the above x-path expression we have specified complete path of the element, hence it is called as x-path by absolute position.
  • The x-path by absolute position will be very lengthy, in order to reduce the length of the x-path expression we use x-path by relative position, where we use double slash (“//”) which means anywhere in the webpage or anywhere inside the root.

Example:?


Relative X-Path:

  • Whenever x-path is written directly to the web element using web element back end attributes is called as relative x-path.
  • Relative x-path will never fail to identify object even though object location change.
  • Here we will be identifying the web elements using the attribute values.

Syntax:?

//html tag [@property name = ‘property value’]

Example:?

// input [@name=’User name’]

  • This is also called as x-path by attribute.

?Note: While specifying the property value, we can use single quotes “ ‘’ ” or double quotes “” , but if we are using the double quotes means we should suppress it using the escape character (‘\’ back slash).

Example: 1. //a [@id=’a1’] and //a [@id=\”a1\”]

Case 1:?

<html>

?????<title> Qspiders </title>

?????<body>

<div>

? <h1>Welcome to Qspiders</h1>

? <input type="text" name="username"/>

? <input type="text" name="pwd"/>

? <input type="button" name="username"/>

</div>

?????</body>

</html>

X-Path:?

//input[@type=’text’ and @name=’username’]

// input[@type=’checkbox’ and @name=’username’]

  • Whenever web element not able to find using one attribute in such cases, we go for multiple attribute using ‘and’ keyword.

Case 2: When similar object is present in UI, how to identify object.

<html>

?????<title> Qspiders </title>

?????<body>

<div class='fs'>

? <img src=''image.jpg' width=100, hight=100/>

? <input type="button" value="buy now"/>

? <input type="button" value="cancel"/>

</div>

<div class='rb'>

? <img src=''image.jpg' width=100, hight=100/>

? <input type="button" value="buy now"/>

? <input type="button" value="cancel"/>

</div>

?????</body>

</html>

X-path:

//div[@class=’fs’]/input[@value=’buy now’]?

//div[@class=’rb’]/input[@value=’buy now’]?

  • Whenever web element not able to identify using multiple attributes, in such cases take a help of parent and grandparent to identify those web element.

Note: We can use multiple attribute in one x-Path.

Example: //html tag[@attribute=’value’ and @attribute=’value’ and @attribute=’value’ and @attribute=’value’……..]

  • There is no limitation in html tag to identify an element.

Case 3: Working with web table.

<html>

?????<title> Qspiders </title>

?????<body>

<tablename ='tab1' border='b'>

? <tbody>

? <tr>

? <td><input type = 'text'/></td>

? <td><input type = 'text'/></td>

? <td><input type = 'text'/></td>

</tr>

<br>

<tr>

? <td><input type = 'text'/></td>

? <td><input type = 'text'/></td>

? <td><input type = 'text'/></td>

</tr>

? ? </tbody>

? ? </table>

?????</body>

</html>

// The table will be created like below, Now if you want to identify element in this table,?


-The x-path will be like before

X-path: //tr[1]/td[2]/input[@type=’text’]

Assignment:?

  1. Write an x-path to identify all the boxes present in the table.

Case 4: working with the multiple static web tables in UI.

  • Consider the following table, which contains the two tables,

  • The html structure for this will look like below.


X-path:?

//table[@name=’tab1’]/tbody/tr[1]/td[2]/input[@type=’text’]

//table[@name=’tab2’]/tbody/tr[1]/td[2]/input[@type=’text’]

//table[@name=’tab2’]/tbody/tr[2]/td[1]/input[@type=’text’]

Case 5: working with dynamic web table.

???Content of the web table is chasing dynamically is called dynamic web table.

Example: Inbox table in g-mail, the content will be keeping on changing.


Html structure will be like as below.


//tr[td[a[@href=’https://msg1.com’]]]/td[1]/input[@type=’checkbox’]

<html>

?????<body>

<title> Qspiders </title>

<table>

<tbody>

? ? <tr>

<td><input type='checkbox'/></td>

<td><a >msg1</a></td>

? ? </tr>

? ? <tr>

<td><input type='checkbox'/></td>

<td><a >msg2</a></td>

? ? </tr>

? ? <tr>

<td><input type='checkbox'/></td>

<td><a >msg3</a></td>

? ? </tr>

? ? <tr>

<td><input type='checkbox'/></td>

<td><a >msg4</a></td>

? ? </tr>

? ? <tr>

<td><input type='checkbox'/></td>

<td><a >msg5</a></td>

? ? </tr>

</table>

</tbody>

?????</body></html>

The UI will look as below

Now, consider I want to select the check box of msg2, but in this check box and the msg2 link are different web element, in the above case I want to select one web element based on another web element.

In the above case, the check box element is depending on the another web element msg2,In this cases we always go for creating the dependencies between the web element using the x-path.

Following are the steps to work with dynamic web table.

The element which is identified with respect to some other element is called as dependent element.

In order to identify the dependent element we follow below mentioned steps.

Step 1: Identify dependent and independent element,

Example: in the above UI, select the check box of msg2, in this example, checkbox is dependent and Msg2 is independent.

Step 2: Inspect the independent element.

Example: Right click on the msg2 link and select inspect with firebug

Step 3: Find the common parent and note down the tree structure, which should contain independent, common parent and dependent element.

Example: Place the mouse pointer on the source code of independent element, which will highlight the independent element in the application/UI, then move the mouse pointer in the up-word direction in the html tree till both independent and dependent elements are highlighted. (It will a common parent) and note down the tree structure

Now, consider I want to select the check box of msg2, but in this check box and the msg2 link are different web element, in the above case I want to select one web element based on another web element.

In the above case, the check box element is depending on the another web element msg2,In this cases we always go for creating the dependencies between the web element using the x-path.

Following are the steps to work with dynamic web table.

The element which is identified with respect to some other element is called as dependent element.

In order to identify the dependent element we follow below mentioned steps.

Step 1: Identify dependent and independent element,

Example: in the above UI, select the check box of msg2, in this example, checkbox is dependent and Msg2 is independent.

Step 2: Inspect the independent element.

Example: Right click on the msg2 link and select inspect with firebug

Step 3: Find the common parent and note down the tree structure, which should contain independent, common parent and dependent element.

Example: Place the mouse pointer on the source code of independent element, which will highlight the independent element in the application/UI, then move the mouse pointer in the up-word direction in the html tree till both independent and dependent elements are highlighted. (It will a common parent) and note down the tree structure.


Step 4: derive the x-path for independent element

Example: //a[@]

Step 5: Using the above x-path, derive the x-path for common parent

Example: //tr[td[a[@]]]?

Note: In the above x-path we are using a [] for navigating back to its parent

??[] used to provide the backend attribute for the web element/ go to parent.

Step6: Using the above x-path derive the x-path for dependent element.

Example: //tr[td[a[@]

Note:?

  • Any given element will have only one immediate parent, hence, while traversing in backward direction, we do not use index, whereas while traversing in the forward direction we may use index because an element can have one or more child elements.??

Write a script to select the msg2 checkbox

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

public class DynamicTable?

{

public static void main(String[] args)

{

WebDriver driver = new FirefoxDriver();

driver.manage().window().maximize();

driver.navigate().to("file:///C:/Users/HP/Desktop/dynamic.html");

WebElement ele = driver.findElement(By.xpath("https://tr[td[a[@href='https://msg2.com']]]/td[1]/input[@type='checkbox']"));

if(ele.isDisplayed())

{

System.out.println("Element is present");

ele.click();

if(ele.isSelected())

{

System.out.println("Checkbox is selected...");

}

else

{

System.out.println("Checkbox is not selected...");

}

}

else

{

System.out.println("Element is not displyed");

}

driver.close();

}

}

Output:

Element is present

Checkbox is selected...

If you look in to the x-path we have written,

//tr[td[a[@]

It just looks like an absolute x-path from parent node,?

And typically in the real time application the position of a web element will also be keep on changing, in that case, the above x-path may fail sometime, coz we are traversing to parent by depending on the parent name, if that got changed means, our x-path will fail, to overcome this instead of using ‘[]’ we use ’ ..’ operator, this will also works same as ‘[]’ i.e it will also go to the immediate parent.

But, advantage of ‘..’ operator is that, even if the name of the parent got changed also it will navigate to its immediate parent, as it is not depending on the name of the parent.

Now, if we write our previous x-path using ‘..’ operator, it looks like below.

//a[@]

Note:?

  • Now you can see the difference between these two x-paths.
  • Typically we always prefer to use the ‘..’ operator over ‘[]’ when we are traversing between the web elements, coz its optimised and more reliable compare to ‘[]’.

Assignment:

  1. Download link of ‘java’ in seleniumhq website
  2. Tour package cost of Europe in makemytrip.com
  3. Price of Samsung galaxy phones in flipkart
  4. Find the phone number of Bangalore which is present on IRCTC.com

X-Path Functions:

  • X-Path allows us to use some in-built functions to identify the web elements on the UI/application and these functions help us to optimise our x-paths.
  • ??X-path supports lots of functions out of which 90% of the time we will be using the following function’s to identify the web elements uniquely.

  1. text()
  2. normalize-space()
  3. contains()
  4. following-siblings()
  5. preceding-siblings()
  6. descendent()
  7. ancestor()

Text () function:

  • text () function is used to identify the web elements which is displaying on the UI.
  • Situation like,
  • When we do not have complete knowledge about the web elements HTML properties.
  • When the values of the attributes are dynamic i.e changing
  • When we would like to identify the elements which is displaying on the UI.
  • When we are in the above situation we prefer to use the text() function.

Syntax: text()=’expected value’

? In x-path we use text() like this: //htmltag[text()=’expected value’]

Example:?

<html>

?????<title> Qspiders </title>

?????<body>

<h1>Qspiders <h1>

<h1 class='sub header'> Welcome to Basavavadi Qspiders </h1>

<img src='imag3.png' width='100' height='100'/>

<a >Qspiders</a>

<input type='button' value='Click'/>

?????</body>

</html>

  • In the above html tree structure, We can identify the x-path as like below using the text function.
  • //h1[text()=’Qspiders’]
  • //h1[text()=’Welcome to Banasavadi Qspiders’]

Example 2: Consider an example of Gmail, will try to identify the ‘create new user’ hyper link using the text function.


//a[text()=’create an account’]

  • Text () function is used to identify any web element using the visible text in UI.
  • Text () function is exact string matching function, which can’t identify object part of a string.
  • Text () function navigate to entire html document and check for the expected value in UI. This returns true if complete string match with UI/actual value.?

Drawback of text () function:

  • It can’t identify dynamic web text. (i.e if the text displaying on the UI is keep on changing means, using text functions we cannot handle that kind of dynamic changing web text)
  • It can’t identify web element using part of a string. Because, it is a string matching pattern.

Ex: //a[text()=’create an’]

  • Here cannot identify, coz, it can’t identify element using part of the string.
  • Text() will identify only those visible text which is exactly string matching.
  • It can’t identify/ignore the spaces before and after the string.
  • Text () function can’t be used with back end attribute.

Example: <a href=”http:gmail.com”>Gmail</a>

Example:?


<html>

?????<title> Qspiders </title>

?????<body>

<h1>Qspiders <h1>

<h1>Welcome to <h1>

<h1>Banasavadi Qspiders<h1>

<input type='button' value='Qspiders'</a>

<div class='gmail'/>

<a >Gmail</a>

</div>

<div class='Yahoo'/>

<a >Yahoo</a>

</div>

?????</body>

</html>

X-path: //div[@class=’gmail’]/a[@href=’https://www.gmail.com’]

Or

//div[@class=’gmail’]/a[text()=’Gmail’]

Normalize-Space function:

  • The?normalize-space?function strips leading and trailing white-space from a string, replaces sequences of whitespace characters by a single space, and returns the resulting string.
  • In text () function, we have a drawback like, if the string has white spaces before and after the string, text () function cannot identify the element. To overcome from that drawback we use the normalize space function.
  • Normalize-space () function removes the spaces before and after the string and it returns the normalised string.

Syntax:

normalize-space ( [string] )

?? Example:

normalize-space(text()/@attribute)

How to use in x-path:

Example:?

//htmltag[normalize-space(text()/@attribute)=’expected value’]


//h1[normalize-space(text())=’Welcome’]

//a[normalize-space(text())=’yahoo’]

//a[normalize-space(@value)=’qspiders’]


Gmail application:


X-path:?

//h1[normalize-space(text())=’one account all of google’]

//a[normalize-space(text())=’Create account’]

//a[normalize-space(@value)=’login’]


  • Normalize function is used to ignore the spaces before and after the string and not in between the string.
  • Using the normalize space function; we can identify the element with backend attribute value of the web element.

//input[@type=’button’ and normalize-space(@value)=’login’]


Contains function:

  • By using 'contains' function in X-Path, we can extract all the elements which matches a particular text value.
  • Contains method is used when we know about the partial attribute value or partial text associated with the web element.



Syntax:

??? Contains(text()/@attribute,’expected value’)

How to use in X-path:

//htmltag[contains(text()/@attribute,’expected value’)]

Example:?

  • To find the “Create an account” hyperlink, consider the following variations of contains() method.

By Text:

//a[contains(text(),’Create’)]

//a[contains(text(),’an’)]

//a[contains(text(),’an account’)]

By Attribute

//a[contains(@id,’signup’)]

//a[contains(@id,’link-signup’)]

//a[contains(@id,’link)]

  • Take a note that two link would be highlighted, thus user would have to supply some additional attribute value in order to locate the link uniquely.
  • For all the above X-paths, user would be able to find “Create an account” hyperlink successfully and the web element would be highlighted.

Note:

  • Contains function is a substring matching function, which can verify web element using part of the string.
  • Contains function navigate to entire html document and check for the expected value, which returns true if string matches expected value partial matches with UI.
  • Contains function can automatically ignore white spaces before and after the string.
  • Contains function play major role to identify dynamic object in UI, while working with ajax.?


Following-Sibling function

  • The following-sibling function selects those nodes that are siblings of the context node (that is, the context node and its sibling nodes share a parent node) and which occur later in document order than the context node.


Syntax:?

/following-sibling::

How to use in x-path:

//htmltag/following-sibling::htmltag


Example:

//img[@src=’image2.png’]/following-sibling::input[1]

Note:

  • Sometimes we go with the index because the position and attribute values keep on changing.

//img[@src=’image2.png’]/following-sibling::input[@value=’Cancel’]

  • Following sibling function is used to identify next immediate sibling from the current html tag.
  • Whenever web element changes its attribute completely in that cases we use following sibling function, to identify dynamic element using sibling as a reference.

Preceding sibling function:

  • Preceding sibling function is used to identify previous immediate sibling from the current node.
  • The preceding-sibling axis selects those nodes which are siblings of the context node (that is, the context node and its sibling nodes share a parent node) and which occur earlier in document order than the context node.

Syntax:

?????/prceding-sibling::

How to use in x-path:

//htmltag/preceding-sibling::htmltag

Example:

<td>

<div class="btn-group">

<button class="btn btn btn-danger block" title="Warning, Delete" name="delete" type="button">

<button class="btn btn btn-default block" title="View History" name="history" type="button">

<button class="btn btn btn-default block" title="View Settings" name="settings" type="button">

<button class="btn btn btn-default block" name="device" type="button">

<span class="glyphicon glyphicon-pencil"/>

?Arcade Reader

</button>

</div>

</td>

X-path:

//button[contains(.,’’Arcade reader’)]/../preceding-sibling::button[@name=’settings’]?


Pooja Kabra(Toshniwal)

Product Owner: QAeverest.ai, FindMYLocator | QA Consultant | Automation tester | Framework Architect

1 年

To get a unique locator with just by single click use "FindMyLocator" https://chromewebstore.google.com/detail/find-my-locator/dcbgkkkekedfhfkopmcpgekglckkgmgj?hl=en

回复

要查看或添加评论,请登录

Srinivas Prasad K T的更多文章

社区洞察

其他会员也浏览了