Basic Detailed about is Locators in Selenium ?
Selenium uses locators to access HTML elements from web pages. These locators have the ability to find and match any elements inside the DOM page. We can use it to perform actions on GUI elements such as radio buttons, buttons, text boxes, links, checkboxes, dropdowns, etc.
Selenium WebDriver provides advanced techniques to locate elements on web pages.
Different Types of Locators in Selenium
Locators available in Selenium along with their descriptions and syntax :-
ID :- Locates an element using its unique ID attribute.
driver.findElement(By.id(“elementID”));
Name :- Finds elements by their name attribute.
driver.findElement(By.name(“elementName”));
Class Name:-Targets elements based on their class attribute.
driver.findElement(By.className(“className”));
Tag Name:-Locates elements by their tag name.
driver.findElement(By.tagName(“tagName”));
Link Text-Specifically used to locate anchor (<a>) elements by their visible text.
driver.findElement(By.linkText(“Visible Text”));
Partial Link Text:-Similar to Link Text, but allows for partial matching of the anchor text or dynamic Link text.
driver.findElement(By.partialLinkText(“Partial Text”));
XPath:-A powerful and flexible locator that uses XML path language to navigate through elements and attributes in the DOM.
driver.findElement(By.xpath(“//tag[@attribute=’value’]”));
CSS Selector:- Utilizes CSS selectors to find elements. This is often faster than XPath and can be very expressive for complex selections.
driver.findElement(By.cssSelector(“cssSelector”));
What is XPath in Selenium?
XPath is a Selenium technique to navigate through a page’s HTML structure.
It enables testers to navigate any document’s XML structure, which can be used on both HTML and XML documents.
While other that locators in selenium search for elements using tags or CSS class names are more straightforward, they may not be sufficient to select all DOM elements of an HTML document.
XPath provides an option to search for an element within a web page dynamically, thus giving sufficient flexibility to tweak a locator to one’s advantage.
The XPath is used to find an element on the webpage, if the element are not found by the general locators like ID, Class, Name, etc., then XPath is used to find an element on the web page.
The basic format of XPath in Selenium :-
XPath=//tagname[@Attribute=’value’]
//: denotes the current node
tagname: denotes the tag name of the current node
@: denotes the Select attribute
Attribute: denotes the attribute of the node
Value: denotes the value of the chosen attribute
What is purpose of Locators or identify the elements?
There are 8 different types of locators support by selenium. If you pick more than one locators will be there , so when to which locators to use and choosing the locators and identify the locators is very important .
Example to Find the element
If multiple Element are matching with given property , the reason is every element start from HTML and will start searching for element line by line and given property .As soon as it find the elements and click the elements it never search remaining elements , Once action is done .
XPath Operators and Functions
Path operators and functions in details covering commonly used XPath defines and handles. XPath defines Operators and functions on Nodes, String, Number and Boolean types.
Comparision operatos
Comparision operators to compare values.
Boolean Operators
Boolean operators to check 'and', 'or' & 'not' functionalities.
Number Function and operators
Operators/Functions on numbers.
String Function
Various string functions.
Nodes function
Various functions and operators acting on nodes.
when to choose XPath?
when elements cannot be found with general locators like ID, name, class, etc. And this is when XPath is used to locate those elements on the webpage.
How do we write XPath?
node name - Select all nodes with name "node name"
/ - select from the root node
// - Select nodes in the document from the current node.
@ - select attributes
Types of XPATH
Types of XPath Locators
The XPath is the language used to select elements in an HTML page. XPath can locate any element on a page based on its ID,,CSS selectors Name ,TagName, ClassName , etc.
There are two types of XPath in Selenium.
Absolute XPath
Relative XPath
What is Absolute Xpath ?
Absolute XPath is the direct way to find the element. But the disadvantage of the absolute XPath is that if there are any changes made in the path of the element then that XPath fails .
How to find absolute XPath ?
The particular URL of which you want to get absolute XPath, Right-click on the logo and inspect it. It will highlight the HTML code of the web element.
Relative XPath
The current element is having any unique attributes which is not supported by selenium locators then, we can choose relative XPath .
When current element not is having any unique attributes , which is not supported by selenium locators the we an check parent element and start writing Relative XPath.
So check any other attributes in this element is unique or not unique
Example:- How to check is it unique ?
copy the value press CTRL+F and check value =male
There are using only one time place which means value property is unique.
How to write relative XPath Example?
//tagname[@attributes='value']=>//input[@value='male']
How to find Parent Elements?
A parent is an element that is directly above and connected to an element in the document tree. Example <div> is a parent to the <ul>. A child is an element that is directly below and connected to an element in the document tree.
Example:-
Happy Learning