What is XPath in Selenium – Table of Content

What is XPath in Selenium?

Selenium is an open-source, most popular web automation testing tool that supports multiple browsers & OS. XPath in Selenium is an XML Path and a syntax useful for locating an element on a web page. Locating any element on the web page uses XML path or XPath expression. Further, XPath in Selenium is useful for navigating through the HTML structure of the web page. 

Moreover, XPath uses HTML DOM structure to find any element on a web page for both HTML and XML documents. 

The syntax for XPath In Selenium 

XPath in Selenium holds the element’s location on the web page. The basic syntax for XML Path Selenium is as follows-

Xpath=//tagname[@attribute="value"]

The meaning of each expression in the syntax is-

  • // : Choose the existing node.
  • Tagname: Particular node’s tagname.
  • @: This symbol denotes the “Select” attribute.
  • Attribute: Node’s attribute name.
  • Value: Attribute’s Value.

Become a master of Selenium by going through this HKR Selenium Training!

XML Document

The XML documents are the text files that contain XML data, including elements and other markups, in a sequential package. Further, it can include a wide variety of data such as numbers databases, numbers of a mathematical equation, etc. You can understand XML document with an example:-

 Kumar

   AK & Co.

    032456123

Here, the above code is divided into two parts- Document Prolog & Document Elements. Let us discuss them in brief.

Document Prolog

The document prolog appears at the top of the document, beforE the root document element. It includes XML and Document type declaration. 

Document Elements

These major building blocks of XML segregate the document into different sections. Each of these document sections perform a particular purpose. Moreover, you can easily segregate a document into different sections so that search engines can use it. Further, these document elements can be the containers having text and other elements combined. 

Types of XPath

  1. Absolute XPath:
  2. Relative XPath:
Absolute XPath

In Selenium, the absolute XPath is the direct path to find the element. This Xpath begins with the “/” (Slash) symbol and helps select the element from the root. The major drawback of this XPath is that if you change the path of the element or attribute, the absolute XPath will fail.

Relative XPath:

The Relative XPath in Selenium begins with the double forward slash “//” symbol and from the middle of the HTML DOM. You can search elements anywhere on the web page as it doesn’t need to write a lengthy Xpath. This XPath is mainly considered as it is not a complete path from the root element.

For example: //input[@id=‘ap_email’]

Suppose You launch Google Chrome and navigate to google.com. Then locate the search bar utilising XPath. By analysing the web element there is an input tag and attributes like class and id. Utilise the tag name and given attributes to create XPath that will locate the search bar.

If you want to Explore more about Selenium? then read our updated article – Selenium Tutorial

What-is-xpath-in-Selenium-1

Click the Elements tab and press Ctrl + F to open a search box in chromes developers tool.  Write XPath string selector and it will try to search based on that criteria. In the image given above, it has an input tag.  //input implies tagname. Use the name attribute and pass ‘q’ as its value. It provides XPath expression as shown below:

//input[@name=’q’]

XPath string

It has focused on the element that implies this specific element was located utilising XPath.

If you want to Explore more about Selenium? then read our updated article – Selenium Tutorial!

Check out our Latest Tutorial video. Register Now Selenium Online Course to Become an expert in Selenium.

                

Acquire Selenium with jenkins certification by enrolling in the HKR Selenium with jenkins Training program in Hyderabad!

Selenium Certification Training

  • Master Your Craft
  • Lifetime LMS & Faculty Access
  • 24/7 online expert support
  • Real-world & Project Based Learning

XPath Functions

Automation utilizing Selenium is unquestionably an incredible innovation which gives numerous approaches to distinguish an article or component on the website page. Be that as it may, in some cases we do deal with issues in recognizing the articles on a page that have similar credits. Some cases can be: components having similar credits and names or with more than one button with similar name and ids. It’s trying to train selenium to distinguish a specific item on a website page and it is the place where XPath functions to serve as the hero. 

Frequently asked Selenium Interview Questions and Answers !!

Types of XPath Functions

Selenium involves different functions. The three of the most broadly utilized functions are given below:

1) Basic XPath

The basic XPath expression selects nodes or a list of many nodes based on various elements or attributes such as ID, Name, ClassName, etc. It selects them from the XML documents. The syntax we can use for the basic XPath is –

Xpath=//input[@name="uid"
2.Contains()

It is a method used in XPath expression when the value of an attribute or element dynamically changes. You can easily find the elements with a partial text using the “Contains” feature in the XPath expression. Now understand this with the below example.:-

Xpath=//*[contains(@type,'sub')]

The above example denotes that the full value of the element type is submitted, but we use the partial text ‘sub’ here to find the element. Thus, in the above example, we tried to find the element by giving a partial text of the attribute “submit”. 

3) Using OR & AND

Here, we use two conditions, first or second condition, among which one condition must be “True” to execute it. This method is still applicable if any one or both conditions are “true”. It means that any conditions should be true to find the element. The expression we can use for this is-

Xpath=//*[@type="submit" or @name="btnReset"]

The above XPath expression will help determine whether a single or both conditions are ‘True’.

Similarly, in the “And” XPath expression, also we use two conditions, but both conditions should be “true” to locate the element. If any one of the conditions becomes “false”, then the expression cannot find the element. The syntax we can use for this function is-

Xpath=//input[@type="submit" and @name="btnLogin"]
4) Xpath Starts-with

The function Xpath-Starts-with() in the Xpath functions is useful to find the element whose attribute value changes in some conditions. Here the value changes with the refresh of the page or by performing dynamic actions on the webpage. In this method, the initial text of the attribute should be in parallel to locate the element whose attribute value changes interactively. 

Further, you can also find the elements whose attribute value doesn’t change or remain static. You can understand this function’s use by the following example:-

Xpath=//label[starts-with(@id,'message')]

The above syntax shows that two different elements start with the initial id “message”. Here, you can use the Xpath-starts with function to check whose attribute value changes or remains static.

Become a master of Selenium by going through this HKR Selenium Training in Delhi!

HKR Trainings Logo

Subscribe to our YouTube channel to get new updates..!

5) XPath Text() Function

In Selenium WebDriver, the function XPath Text() is a built-in function useful to locate elements based on the web element’s text. Using this function, you can find the same text element. Moreover, the elements that you locate must be in a string format.

Xpath=//td[text()='UserID']

Using the above expression having text function, you can locate the element that will show the exact match of the text.

6) XPath axes methods

This method in XPath functions is useful for finding complex or changing elements. However, we can see the following XPath axes methods which we can use:-

  1. a) Following- It is useful to select all the elements in the document of the existing node(). The expression you can use for this method is-
 Xpath=//*[@type="text"]//following::input
  1. b) Ancestor- The ancestor axes method is useful to select all the ancestor elements of the existing node, like parents, grandparents, etc. Here, the expression you can use is-
Xpath=//*[text()='Enterprise Testing']//ancestor::div
  1. c) Child- This axes method selects all the child elements in the documents’ current node. The expression you can use here is-
Xpath=//*[@id='java_technologies']//child::li
  1. d) Preceding- This method helps select the nodes that come before the existing ones. Here is the example expression:-
Xpath=//*[@type="submit"]//preceding::input

The above expression helps to identify all the input elements before the currently given nodes.

  1. e) Following-sibling- This method helps to select the following siblings of the existing node. All the siblings will be equivalent to the existing node, and the method will find the sibling next to the existing node. Moreover, the syntax you can use here for this method is-
xpath=//*[@type="submit"]//following-sibling::input
  1. f) Parent- It helps to select the parent from the existing node of the element. The following is the syntax you can use here.
Xpath=//*[@id='rt-feature']//parent::div

Many div(s) match with the parent, but if you want to focus on a specific element. For this you can use the below xpath syntax-

Xpath=//*[@id='rt-feature']//parent::div[1]
  1. g) Self- In this method, it selects the existing node where it selects itself only. That means the node here is the “self”. The expression you can use for self is-
Xpath =//*[@type="password"]//self::input
  1. h) Descendant- It helps to select the descendants of the existing element where it recognizes all the element descendants of the existing element. 
Xpath=//*[@id='rt-feature']//descendant::a

Selenium Certification Training

Weekday / Weekend Batches

Conclusion

XPath or an XML Path is used to locate any element or navigate through the HTML structure of a webpage. It is generally used for automation purposes and in cases where it is difficult to find elements using locators like name, class, ID, etc. However, it is the most important among the locators useful in Selenium to identify web elements. Also, it is a handy locator for the testers of web pages.

Thus, learning about XPath in Selenium will help you quickly identify a web element on a web page. 

Related Articles:



Source link

Leave a Reply

Subscribe to Our Newsletter

Get our latest articles delivered straight to your inbox. No spam, we promise.

Recent Reviews


Last updated on
Jun 12, 2024

SQLite vs PostgreSQL – Table of Content

What is SQLite? 

SQLite is an auto, file-based, and completely open-source relational database management system (RDBMS) that is noted because of its mobility, reliability, and excellent performance even when in low-memory applications. Even if the system fails or there is a power outage, its transactions are ACID-compliant. The SQLite project touts itself as a “serverless” database on its website. Typical relational database systems are deployed as a server process, with programs communicating with the host server via interprocess communication. SQLite, on the other hand, enables any system that utilizes the databases to read and write directly to the database disc file. This makes it easier to set up SQLite because it eliminates the requirement to set up a server process. Similarly, apps using the SQLite database don’t need to be configured; everything they need is to access.

What is PostgreSQL? 

PostgreSQL, or Postgres, describes itself as “the world’s most sophisticated open-source relational database.” It was built with the intention of being highly expandable and consistent with industry standards. PostgreSQL is indeed an object-relational database, which means that while it’s essentially a relational database, it also has features that are more commonly associated with object databases, such as table inheritance and feature overloading. Concurrency is a feature of Postgres that allows it to efficiently handle numerous processes at the same time. It does so without using read locks because it uses Multiversion Concurrency Control (MVCC), maintains the synchronization, coherence, exclusivity, and durability of its transactions, often known as ACID compliance. Although PostgreSQL isn’t as popular as MySQL, it still has a variety of third-party libraries and tools, such as pgAdmin and Postbird, that make dealing with it easier.

Take your career to next level in PostgreSQL with HKR. Join PostgreSQL online training now

Difference between SQLite and PostgreSQL

However both SQLite & PostgreSQL are available as open Relational Database Management Systems (RDBMS), there may be a few distinctions to consider when picking which one to utilize for your company. The following are the significant distinctions that influence the SQLite vs. PostgreSQL decision:

Database Model
  • SQLite is indeed an embedded database management system. This means it’s a Serverless DBMS that can be used within your apps.
  • To set up and run across a network, the PostgreSQL DBMS uses a Client-Server Model thus needs a Database Server.
Setup Size
  • SQLite is much smaller than PostgreSQL, with a data size of less than 500KB. Its installation files are over 200MB in size.
Data Types Supported
  • INTEGER, NULL, BLOB, TEXT, & REAL are the only data types supported by SQLite. In SQLite, the phrases “data type” and “storage class” are interchangeable.
  • PostgreSQL, on either hand, can store almost any type of information that you could need to put in your database. This could be an INTEGER,  CHARACTER, SERIAL, VARIABLE, or something else entirely.

PostgreSQL Training

  • Master Your Craft
  • Lifetime LMS & Faculty Access
  • 24/7 online expert support
  • Real-world & Project Based Learning
Portability
  • SQLite keeps its database as a single conventional disc file that may be found anywhere in the directory. The file is also saved in a cross-platform form, making copying and moving it a breeze. SQLite is among the most transportable Relational Database Management Systems because of this (RDBMS). PostgreSQL, on either hand, is only portable when the database is exported to something like a file and afterward uploaded to a server. It can be a time-consuming task.
Multiple Access
  • When this comes to user management, SQLite falls short. This also misses the capacity to control several users accessing the system at the same time.
  • PostgreSQL is excellent at managing users. It provides well-defined authorizations for users, which decide which database actions they are allowed to do. It can also support numerous users accessing the system at the same time.
Functionality 
  • Because SQLite is indeed a simple database management system, it includes basic capabilities that are appropriate for all sorts of users. PostgreSQL, on either hand, is a sophisticated database management system with a wide range of capabilities. As a result, users can accomplish a lot more using PostgreSQL than they can with SQLite.
Speed
  • SQLite is quick given the fact that this is a lightweight database management system having simple operations and a minimalist design.
  • PostgreSQL might not have been the best database for quick read queries. This is due to its sophisticated design as well as the reality that this is a large database management system. It is, nevertheless, a robust database management system for conducting complex processes.
Security Features 
  • Authentication is not included with SQLite. Anyone with database access has the capacity to read and modify the database file. It renders it inefficient for storing sensitive and private information. Many security features come included with PostgreSQL. It also necessitates extensive configurations from its users in order for it to be secure. As a result, PostgreSQL is a secure database management system for storing private and sensitive information.
HKR Trainings Logo

Subscribe to our YouTube channel to get new updates..!

Features of SQLite 

  • Small footprint: The SQLite module is quite light, as its name implies. Although the amount of space it takes up fluctuates based on the system on which it is installed, it can be less than 600KiB. Additionally, SQLite is completely self-contained, which means you don’t need to install any extra dependencies for it to work.
  • SQLite is known for being a “zero-configuration” database that is ready to use right out of the box. SQLite doesn’t operate as just a server process, so it doesn’t need to be halted, restarted, or resumed, and it doesn’t arrive with just about any configuration files to handle. These capabilities make the process of installing SQLite and incorporating this with an app much easier.
  • SQLite is an excellent database choice for embedded applications that require portability but do not require future expansion. Single-user local apps, mobile applications, and games are examples.
  • A whole SQLite database is kept in a single file, unlike many other database systems, that often store data as a vast batch of distinct files. This file could be transferred through external devices and file transfer protocol and can be found everywhere in a directory structure.
  • Testing: Using a DBMS that utilizes a dedicated servers process to test the functionality of multiple applications can be excessive. SQLite features an in-memory mode that allows you to run tests rapidly without having to worry about the expense of entire database transactions, making it an excellent choice for testing.
  • SQLite can be used as a disc access alternative in circumstances in which an app wants to study and modify files to disc directly. This is because SQLite has more capability and is simpler to use.

Features of PostgreSQL

  • PostgreSQL, more than SQLite, strives to follow SQL standards to the letter. PostgreSQL offers 160 of the 179 characteristics needed for proper core SQL:2011 compliance, as well as a vast range of optional capabilities, as per the authorized PostgreSQL documentation.
  • Community-driven and open-source: The source code for PostgreSQL is created by a huge and dedicated community as a fully open-source project. Likewise, the Postgres society preserves and provides a number of online resources that explain how to use the database management system, such as the official paperwork, the PostgreSQL website, and several online forums.
  • Extensible: PostgreSQL’s catalog-driven operation and dynamic loading allow users to enhance it dynamically and on the fly. An object code file, including a shared library, can be designated.
  • Data consistency is critical: PostgreSQL has indeed been completely ACID-compliant from 2001 and uses multi-version monetary control to guarantee data consistency, making it an excellent option of RDBMS where data consistency is crucial.
  • PostgreSQL is interoperable with a wide range of computing languages and systems. This means that migrating your database to a different operating system or integrating it with a specific tool will be simpler with such a PostgreSQL database compared with some other database management system.
  • Complex operations: Postgres provides query strategies that make use of several CPUs to speed up query processing. This, together with its extensive support for numerous simultaneous writers, makes it an excellent candidate for data warehousing and other complex tasks.

Click here to get latest PostgreSQL interview questions and answers

PostgreSQL Training

Weekday / Weekend Batches

Conclusion

SQLite and PostgreSQL,  are the most widely used open-source relational database management platforms today. It has its own set of characteristics and limits and shines in specific situations. When choosing an RDBMS, there are many factors to consider, and the decision is rarely as straightforward as selecting the quickest or most feature-rich option. If you require a relational database system in the future, do some study on these and other technologies to identify the one that best fits your needs.

Related Article:



Source link