Xpath in selenium for a Web application

Tram Ho

The content of the article includes:

  1. The Xpath concept
  2. Classification of Xpath
  3. The structure of Xpath
  4. Detailed explanations of the elements in the structure and examples
  5. Some technology in Xpath

1. The Xpath concept

XPath is an XML path. It is a syntax for finding any element on a web page using the XML path expression. XPath is used to find the locator of any element on a web page using the HTML DOM structure.

2. Classification of Xpath

2.1. Relative xpath

  • The element can be searched anywhere on the Web
  • Can start from any position of HTML
  • Keep it short and very flexible
  • Start with the character: “//”

2.2. Absolute Xpath

  • Searching directly from an element
  • Start with “/”
  • The writing structure is long and not flexible

3. The structure of Xpath

//tagname[@attribute=value]

3.1. Symbol “//”

//: is the instance starting from the current node

3.2. Element => tagname

Textbox => input

Textarea => textarea

Dropdown List (default) => select> option

Dropdown list (Custom) => ul> li div> span

Button => button input

Checkbox => input

Radio button => input

Image => img

Link => a

Table => table

header (table) => th

Column (table) => td

Row (table) => tr

Text => div span label

Header => h1 – h5

Slider => input

Upload file => input

Tooltip => div

Menu => a

Icon => span

3.3. Attribute

  • Attribute is the name of the Attribute

Example: In the FirstName field of the registered function in the page: http://live.demoguru99.com/index.php/customer/account/create/

Attribute name = id

3.4. Value

  • Value is the value of the attribute

Example: In the FirstName field of the registered function in the page: http://live.demoguru99.com/index.php/customer/account/create/

Attribute value = firstname

=> Xpath: //tagname[@attribute=value]

In this example, this will convert to the following:

// input [@ id = ‘firstname’]

4. Some technology in Xpath

4.1. When the Attribute value is unique

  • Use the format: Tagname & Attribute & Value

//tagname[@attribute=value]

  1. // input [@ id = ’email’]
  2. // input [ @title = ‘title = “Email Address”]
  3. // input [ @name = ‘login [username]’]

4.2. When an element cannot be identified by its tag and attribute

4.2.1. Parent node.

  • Using the structure:

For example:

Using the Tagname and attribute we cannot find the uniqueness of the element:

At this point we need to get more conditions from the father’s life downwards

4.2.2. Taken from father’s life upwards

4.3. Get absolute

4.3.1. Text ()

For example:

4.3.2. @attribute ()

4.4. Get relative

CONCLUDE

The article content helps you to capture the element in many different ways in specific cases, next article I will write four more ways to catch the element with Xpath and introduce CSS in selenium to you.

Thank you for reading this article!

Share the news now

Source : Viblo