Session 22 – Selenium with Java|Locators(ID,Name,LinkTe… — Transcript

Learn Selenium locators like ID, Name, LinkText, PartialLinkText, ClassName, and TagName to uniquely identify web elements for automation.

Key Takeaways

  • Locators are fundamental for web automation to identify elements uniquely.
  • Selenium supports both basic and customized locators for flexibility.
  • Basic locators are easier to use but may not always be reliable due to dynamic attributes.
  • Customized locators like XPath and CSS selectors provide more control and adaptability.
  • A good understanding of HTML is essential to create effective locators.

Summary

  • The session covers the importance of locators in Selenium for identifying and interacting with web elements.
  • Locators are essential to uniquely locate elements on a webpage before performing any actions.
  • Selenium provides two types of locators: basic locators and customized locators.
  • Basic locators include ID, Name, LinkText, PartialLinkText, ClassName, and TagName, which are directly supported by Selenium's By class.
  • Customized locators like XPath and CSS selectors are created manually and are useful when basic locators are insufficient or dynamic.
  • Understanding HTML basics such as tags, attributes, and values is necessary to effectively use locators.
  • Different scenarios require different locators; the goal is always to uniquely identify the element.
  • Actions on web elements can only be performed after locating them correctly.
  • Attributes like ID or Name may change dynamically, making customized locators more reliable in some cases.
  • The session emphasizes flexibility in choosing locators based on familiarity and scenario.

Full Transcript — Download SRT & Markdown

00:06
Speaker A
So in today's session, we're going to learn locators, which is a very important and also a very big topic.
00:31
Speaker A
elements on the web page so once you identified or locate an element then we have to perform certain action on that element so these are the only two things we have to learn from automation because in our web page you can see different type of elements various types of elements and we have
00:49
Speaker A
First of all, we need to understand what a locator is. Throughout automation, there are only two things that are most important. First, we have to identify the elements or locate the elements on the web page.
01:08
Speaker A
that there are n number of elements are there in the web page and you should able to locate a specific element on which you want to perform the operation and that is possible only through locators and S name is provided various types of locators to locate web elements and we will try
01:27
Speaker A
Once you identify or locate an element, then you have to perform certain actions on that element. These are the only two things we have to learn from automation because on our web page, you can see different types of elements, various types of elements, and we have to locate them uniquely.
01:44
Speaker A
to locate an element how to perform the action then you'll able to automate the test cases okay so the first part is locators so we need to understand what are the locators are provided by selenium and how can we locate a web elements using locators but before using locators we need
02:04
Speaker A
Let's say you have two or three input boxes, and whichever input box you want to perform an action on, you want to locate exactly the same input box. Similarly, if you have a number of checkboxes, you have to locate a specific checkbox on which you want to perform an action.
02:24
Speaker A
from HTML and then we will able to work with the locator so the basic knowledge is required from HTML okay so now so what are the different type of locators supported in selenium so especially selum supported different type of locators and we can categorize into two parts so basic locators and
02:47
Speaker A
Like that, there are a number of elements on the web page, and you should be able to locate a specific element on which you want to perform the operation. That is possible only through locators. Selenium provides various types of locators to locate web elements, and we will try to understand them.
03:08
Speaker A
link text class name tag name so these are all basic locators and these locators are directly supported by selum through methods like when I use by class The Bu class there are some static methods are there like ID name link text partial link text class name tag name you can find all the
03:28
Speaker A
The second thing is actions. To perform the action on the web element, again, there are many methods available. Once you locate an element, we can perform different types of actions on different types of elements. That also we have to understand.
03:47
Speaker A
class name tag name we use these type of locates the different scenarios for different purposes and we can find everything in HTML itself all these basic locators we can directly find in HTML itself but sometimes ID may be there sometimes name will be there ID may not be there okay and
04:07
Speaker A
Once you know how to locate an element and how to perform the action, then you'll be able to automate the test cases.
04:25
Speaker A
things we can find in HTML itself if you look at an HTML of the page you can find all these things directly on HTML but what is customizer locator so customizer locators we can generate or we can create our own locators and we cannot directly find them in HTML and whatever Properties or
04:47
Speaker A
So the first part is locators. We need to understand what locators are provided by Selenium and how we can locate web elements using locators. But before using locators, we need to understand some basic stuff from HTML because web pages are mostly designed using HTML.
05:09
Speaker A
which is customizable suppose if there is any one element if there is element and somebody's written X paath for that element and somebody else is also return expath for that element both are may not be the same so some guy return expath by using some properties other guys written expath for
05:27
Speaker A
HTML is a scripting language, Hypertext Markup Language. We need to understand some very basic stuff like what is a tag, what is an attribute, and what are the values. We should be able to recognize them from HTML, and then we will be able to work with locators.
05:48
Speaker A
approaches so many attributes so many methods we can use so we can call them as a customizer locators we cannot directly get it from HTML just like a normal basic locators so ID name these are all available as a Properties or as a attribute for the element class name also is available as
06:07
Speaker A
So basic knowledge is required from HTML.
06:27
Speaker A
between basic locators and customized Loc s now the purpose is same whether it is a basic locator or customized locator the ultimate goal is what we need to locate an element uniquely or you need to identify the web element uniquely that is the ultimate goal now another question why these many
06:48
Speaker A
Okay, so now, what are the different types of locators supported in Selenium? Selenium supports different types of locators, and we can categorize them into two parts: basic locators and customized locators.
07:07
Speaker A
we need to consider suppose if there is any one element is there for that element ID attribute is there so you can use ID as a locator or if you want to write an X paath for the element you can
07:18
Speaker A
The purpose of a locator is to identify or locate an element. That is the purpose of a locator. There are two kinds of locators provided: basic locators and customized locators.
07:30
Speaker A
you can write CSS or Expo also okay there is no rule like you have to use only this particular locator here in this particular situation or in this particular locator in this situation it's not like that so whichever locator you familiar with you can use that locator for that element
07:49
Speaker A
Basic locators mean ID, name, link text, partial link text, class name, and tag name. These are all basic locators. These locators are directly supported by Selenium through methods like those in the By class.
08:07
Speaker A
web pages even though you have direct uh locators like class name tag name or name or ID still they may not able to locate a web elements in those cases you can choose for expart CSS but why they
08:23
Speaker A
When I use the By class, there are some static methods like ID, name, link text, partial link text, class name, and tag name. You can find all these methods in the By class itself. These are direct locators provided by Selenium, and we can easily find them in HTML.
08:42
Speaker A
when you're refreshing the page the attributes are keeps changing name is something now after a few seconds or after a few minutes name is different so in those cases you cannot use static attributes as a locators then you need to choose for either CSS or xath we can write our locators more Dynamic
09:01
Speaker A
Suppose there is an element which has an ID attribute; we can use ID as a locator. If we have a name attribute, we can use the name attribute. Link text and partial link text are used especially for dealing with links. Class name and tag name locators are used especially when you are working with multiple web elements or groups of web elements.
09:19
Speaker A
particular locator okay that is a basic context but which one we have to use idea or name you can use anything okay you can use anything any uh any type of locator you can use but ultimate goal is you should able to locate an element uniquely that's the main uh goal of using locators okay so
09:41
Speaker A
Different scenarios require different types of locators, but all these things we can find in HTML itself.
10:01
Speaker A
directly find in HTML itself then how to see the HTML how to see the attributes or properties in HTML how to uh recognize which is dag name which is attribute which is value so how can we get from
10:17
Speaker A
But what is a customized locator? Customized locators are locators we can generate or create ourselves. We cannot directly find them in HTML. Whatever properties or attributes are available for that element, by using those attributes, we can generate our own selectors like XPath or CSS.
10:30
Speaker A
can see inspect and it will show you Dev tools window so this is called Dev tools window on the elements tab you can see the HTML and this is the page source of this particular page okay here you
10:43
Speaker A
These locators are customized locators. We cannot directly find them from HTML itself. We have to create our own locators or generate locators, which are customizable.
10:56
Speaker A
an attribute title is another attribute alt is another attribute class is another attribute if you look at here I don't see ID I don't see uh name or anything right so in this particular case we choose X paath for the Del okay similarly if I look at this search button search is a input
11:16
Speaker A
Suppose there is an element, and somebody has written XPath for that element, and somebody else has also written XPath for that element. Both may not be the same.
11:34
Speaker A
look at this name is available ID is not available so you can use name as a locator to locate a element but you cannot use type you cannot use placeholder you cannot use class directly okay so you need to recognize them properly what is the tag name what are the attributes what are their
11:52
Speaker A
One person may write XPath using some properties, and another person may write XPath for the same element using some other properties. These are customizable, user-defined locators.
12:13
Speaker A
let us start with the basic locators ID and how to use name attribute as a locator and then we'll discuss about link text and then partial link text and after that I will discuss about class name and tag name so these uh locators especially we use to locate specific web elements a single web elements
12:37
Speaker A
We can write our own locators according to our knowledge. CSS and XPath come under customized locators.
12:54
Speaker A
that okay let me show you practically how can we use these locators to locate web element so for that I'm taking this application as an example and I'm going to write a small piece of code uh in which let us try to use locators and in the previous class we have discussed about whichever
13:14
Speaker A
Again, these two have many features, many approaches, many attributes, and many methods we can use. We can call them customized locators because we cannot directly get them from HTML just like normal basic locators.
13:36
Speaker A
that we have to pass how to pass the locator using by class byy do ID or buy. name byy do link text all these methods are available inside the buy class so whichever attribute you are using you can just call that particular method and provide that value in the bracket okay so here this is
13:58
Speaker A
ID, name, class name, and tag name are all available as properties or attributes for the element. Link text and partial link text can also be directly obtained from HTML.
14:14
Speaker A
web driver project and create a new package for today so I'm creating new package package name is day22 okay so in this I'm going to create a new class I'll give some name to this I say locators demo I'm taking main method Also let's say finish okay so first thing let me try to use
14:46
Speaker A
But we cannot directly get CSS and XPath from HTML. We have to generate them, or we can automatically or manually write our own XPath or CSS.
15:04
Speaker A
my browser we need to create an instance of web driver that's the first step let us create web driver driver equal to new Chrome driver this is a statement so we need to import required packages and classes so first we need to import this uh Chrome driver so sometimes it is not showing as
15:34
Speaker A
That is the major difference between basic locators and customized locators.
15:57
Speaker A
that earlier web driver is an interface chome driver is a class so we have to import required packages now we create a driver instance so with this this will launch our Chrome browser and then we have to open the URL of the application so for that what is the method we have to use what is our
16:16
Speaker A
Now, the purpose is the same whether it is a basic locator or a customized locator. The ultimate goal is to locate an element uniquely or identify the web element uniquely. That is the ultimate goal.
16:36
Speaker A
Windows smaller size now I want to maximize this browser window how to maximize the browser window there is another command yesterday we discussed dri dot manage method dot again we have to call another method window do maximize remember this driver. manage. window. maximize so this command
16:57
Speaker A
Now another question: why are so many locators supported to locate an element, and which one should we use?
17:19
Speaker A
some text inside this box I want to pass some text inside this box so we need to locate this element if I inspect this element like this and see what are the attributes are available for this element you can see the name attribute is available all right so name attribute is available so can we
17:38
Speaker A
There is no rule that you have to use only a particular locator. You can use any type of locator with which you are familiar. The ultimate goal is whether you are able to locate any element or not. That's the only thing we need to consider.
17:54
Speaker A
have to take the name method we have to call name and here we have to pass the name name of the attribute which we have captured from HTML okay so this will return web element on top of it
18:08
Speaker A
Suppose there is an element with an ID attribute. You can use ID as a locator, or if you want to write an XPath for the element, you can still write XPath using the ID attribute. You can use CSS also.
18:26
Speaker A
called Sy keys in this we can pass and some product name I say mac I'm passing so this is a way we can locate any element by using name locator and if the name attribute is available then only we can use name if ID is available then only can use ID otherwise you cannot
18:47
Speaker A
If there is a link element, you can use link text or partial link text locator, or you can write CSS or XPath also.
19:11
Speaker A
okay fine now let us see uh some other element how to locate element using ID let me comment this now if suppose ID is available for that element so then how can when we locate an element if ID is available okay so for example let's say here the same
19:36
Speaker A
There is no rule that you have to use only this particular locator in this particular situation or this particular locator in that situation. It's not like that.
20:04
Speaker A
attribute let me check is there an element with ID element ID anything so for example let me find something else so if the ID is available we will able to locate a web element okay so you can see here there is
20:48
Speaker A
Whichever locator you are familiar with, you can use that locator for that element.
21:08
Speaker A
actually this is a logo element here this is ID so now with this ID I just want to locate the element so what action we can perform on the logo here whether it is displayed or not we can
21:21
Speaker A
But why are so many locators provided by Selenium? Sometimes a locator may not work for an element.
21:35
Speaker A
back you can say driver dot find element by dot ID we have to call ID method in this we have to pass the value of the ID now this is a locator this is the element which will return and what
21:52
Speaker A
For example, let us say the ID attribute is there for that element, and even though you use ID directly, sometimes it may not locate the element if you are working with complex web pages.
22:12
Speaker A
false if it is return true means the logo is displayed if it is return return false means logo is not displayed so this will return return a value Boolean value so here I can say logo display status now the type of the variable is what Boolean variable because e
22:37
Speaker A
Even though you have direct locators like class name, tag name, name, or ID, still they may not be able to locate web elements.
22:58
Speaker A
any type of element we can use that ID directly to locate an element let's run this okay so on the UI we have not done anything but it will just return true or false so if the logo is present it is returning true so this is how we can simply use ID locator if ID is
23:29
Speaker A
In those cases, you can choose XPath or CSS.
23:53
Speaker A
we need to understand what is link text and what is partial link text okay so for example here in this particular uh application uh let us say here there are so many elements I can see right so here
24:14
Speaker A
But why may they not be able to locate an element even though we are using the right properties or right attributes?
24:34
Speaker A
representing link anchor tag in HTML wherever you can see a that is always representing a link and which is associated with one attribute called hre href is nothing but hyperlink reference so when I click on this link it will go to this particular page you can see this is the link of
24:54
Speaker A
Because the attributes keep changing; they are not fixed. Sometimes, suppose today the element ID is something, and tomorrow the same element's ID is totally changed.
25:16
Speaker A
this is called link text which is called link text every link you can see the same thing okay for example if I look at this so here the desktops is a link text desktops is a link text okay so there
25:33
Speaker A
When you refresh the page, the attributes keep changing. The name is something now, and after a few seconds or minutes, the name is different.
25:56
Speaker A
is HF attribute will be there and sometimes you can't see HF attributes and the tag is different tag will be input or D or whatever and you can see same kind of a text that is not link text
26:11
Speaker A
In those cases, you cannot use static attributes as locators. Then you need to choose either CSS or XPath.
26:29
Speaker A
link in HTML also you can see the same thing so these are all called link texts so if you want to locate any link on the web page you can use that link text in a locator okay for example I
26:44
Speaker A
We can write our locators in a more dynamic way, and even though the element attributes or properties keep changing, we will still be able to locate web elements.
26:57
Speaker A
want to click on this so before clicking on this tablets I want to locate this element so when you inspect this element right click and inspect this is a anchor tag href attribute and Link text is available here so let me capture this link text link text is not an attribute of the element
27:17
Speaker A
So that is the reason.
27:39
Speaker A
find element by dot here you can see there are two methods link text and partial link text so first let us try to use Link text so when I use Link text you have to pass the complete value of the
27:54
Speaker A
link text the full text value you have to pass so this will able to locate a web element even this also will return web element even link is also web element but what is the action we perform on this
28:06
Speaker A
link we have to perform click action so I can say dot click so this will perform the click action on the link element so by using link text we had just pass link text here so when I run this this will
28:24
Speaker A
perform click action on the tablets link and then it will go to the Target page now we can see it is successfully click on the tablets now it will go to Target page but what is link text what is the
28:40
Speaker A
difference which is most popular interv question also what is the difference between link text and partial link text which one will be preferred to use so we understood what is link text link text means we have to capture the link text from the HTML this is the link text and we provided the
28:59
Speaker A
full text here but when I say partial link text we no need to pass the complete text value you can just pass some partial value so for example instead of Link text I'm calling partial link text Dot partial link text if you're calling partial link text you no need to pass the complete value
29:23
Speaker A
tablets okay so simple you can pass some portion of the value like you can pass tab or you can pass tablet like this or you can pass BLS some portion of the text it is not full link text actually this
29:42
Speaker A
is the part of the actual link text so here I'm passing something called table just I'm passing only table but actual link text is what tablets but here I'm just passing only table so this will also able to look at element and will perform the action so just observe I'm running as a Java
30:04
Speaker A
application okay now you can see it is also done the same job and here the actual link text is what tablets but here we use just a small portion of the text table still it is able to locate
30:20
Speaker A
an element but what is the problem with this we always try to use only link text we don't prefer to use partial link text okay but why is it so when I use partial link text this may match with multiple web elements because we are not using the complete text value right we are
30:43
Speaker A
using some portion of the value let's say I'm using table and this may match with some other links on the same page then it will be conflict there may be multiple Elements which are having link text and those link text is also having same text right so in those cases you may not be
31:03
Speaker A
able to locate element uniquely that is a problem with the partial link text so that is the reason partial link text will not be preferable when I identifying or when I locating the links elements so always try to prefer only link text in the link text we will provide the complete value so that it
31:25
Speaker A
will go and uniquely identify the web element so so what is the difference between link text and partial link text link text we have to provide the complete link text and when I use partial link text we can pass some partial text of the link that is the only difference and which one
31:45
Speaker A
we will prefer only link text we have to prefer okay so that's the reason I'm just saying don't prefer partial link text so there is no question like which one we have to you can use both you can use either link text or you can use partial link text both options are provided but which one
32:04
Speaker A
is preferred is link text is preferred when you have to use partial link text suppose if you do not have any other links which are having this text okay suppose I have a two links and in the both the two links you have the same text in that case you don't prefer this okay if both to links
32:25
Speaker A
are different then you have to prefer this okay so for example let me just tell suppose in your web page I'm taking same example tables there is a link available tablets there is another link is available okay now in this particular case I want to locate I want to locate tables I want to locate
32:51
Speaker A
only tables two links are there assume there are two links I want to locate tables which one you will use partial link link text or link text when which one you will use Link text or partial link text when I use partial link text what is a problem in this that will also match second
33:12
Speaker A
link right that will also my second link that is a problem so when you have to use Link text if you're able to locate element uniquely then you have to use Link text directly okay suppose if you have multiple links and you can see the tables is also part of here the table part here it is
33:32
Speaker A
there here it is there in those cases you have to use only link text link text is preferred in this partial link text is not preferred when I use partial link text it will be the same okay so
33:45
Speaker A
this is a scenario link text is always preferable partial link text is not preferable but if you have single link on the web page Suppose there is only one link like this and there is no other link which is having table there is no other link which is having table then you can use this table
34:05
Speaker A
as part of partial link text that is enough for this okay so basic difference is in the link text we have to pass full text value if it is a partial link text we have to just pass some portion of
34:19
Speaker A
the value and when we have to prefer partial link text if there are some link and there is nothing is common in those links they can use partial link text okay suppose I have a submit button okay and
34:34
Speaker A
I have another button called send button submit and send button here you can use partial link text because nothing is common in these two except this first character nothing is same in this both right so you can use partial link text partial link text of sub you can specify partial link text send you
34:55
Speaker A
can specify both are totally different so in these cases you can prefer partial link text but in this particular case always try to prefer link text as you understood here in this particular case refer to use only link text because nothing is common in both but here both are totally different so
35:16
Speaker A
you can use partial link text partial link text so this is the scenario right so we have have seen how to use name how to use ID how to use Link text and partial link text so these are all the
35:35
Speaker A
basic type of locators which we can use to locate specific web elements or single web elements okay now we also have two more locators class and tag very very important listen this very carefully tag and class tag name and class so these are the two locators which we can directly find in HTML just
36:04
Speaker A
like other attributes tag name and class also we can directly find in HTML but the tag name and class we will use to locate multiple web elements or group of web elements not for specific web elements what is the requirement because tag name can be same for multiple elements if I look at
36:30
Speaker A
this page see there are so many La tags are there La tag name is same for all these elements right and you can see multiple D tags also you can see D tags in many places you can see D tag in many
36:43
Speaker A
places so most of the times tag names will be more means multip multiple elements are having same kind of a diagram Suppose there are five images on this web page all are having same tag name which is IMG and there are multiple links are in this page every link is having same tag name which is
37:07
Speaker A
a anchor tag there are multiple input boxes are there in all input boxes having same tag name which is input same multiple buttons are there in the same page all buttons are having same tag which is button tag so the tag name will be same for group of elements or multiple web elements
37:30
Speaker A
so we cannot use Tag name locator for locating specific web element it is for identifying or locating group of web elements same thing for class also class is nothing but a category of elements suppose if you find multiple checkboxes all the checkboxes are having same value for
37:54
Speaker A
class attribute and suppose the class attribute is available for radio buttons the value of the class attribute is same for all radio buttons suppose if you find multiple drop-downs on your web page all the drop downs are having class attribute but the value of class attribute is same for all the drop
38:12
Speaker A
downs so the class is nothing but a category of elements so same kind of elements are having same value for class attribute that is the reason even this locator also we will try to use to locate group of elements or multiple web elements so tag name and class name so these locators especially
38:35
Speaker A
used to get group of web elements not a specific web element okay remember that point but what is a use what is a requirement to get group of elements we always need to locate a specific web element right we will perform action on the web element we always try to get specific web element but why
38:57
Speaker A
we need to get group of web element what is the requirement for that sometimes we need to do some validations on group of web elements for example on this particular web page I want to find how many total number of links are there or I want to capture all the links from this web page I want to
39:18
Speaker A
count total number of links on this web page and then I will compare with my expected number of links suppose I'm expecting 100 links should be present on this web page how can we validate so manually it is very difficult thing right because there are so many links are there we cannot count
39:35
Speaker A
each and every link and sometimes there may be some hidden links also there they are not visible on the page so in that case we are we want to capture all the links from the web page group of
39:48
Speaker A
links from the web page group of elements from the web page in that case we will use something called tag name or class and suppose suppose I want to find total number of images on this page how many
40:02
Speaker A
total number of images are present so how can we count we need to capture all the image elements every image is a web element so we need to capture all the images from the web page and then we will
40:13
Speaker A
able to count it so in that particular scenarios we want to work with the group of web elements if you want to capture group of web elements we want to use a common thing there should be some common
40:26
Speaker A
thing should be there for every element and that common thing is what either it can be tag name or class so these things are commonly there for group of web elements so tag name and class name will be used whenever you want to capture group of web elements whenever you want to work with
40:46
Speaker A
the group of web elements so remember this point so for specific element for Unique element you cannot use Tag name or class tag name or class will be used for getting group of web elements now I will show you some examples so on this particular web page okay my requirement is uh I
41:08
Speaker A
want to capture I want to capture all this header related links like here is currency phone number and this my account my wish list shopping cart checkout there are some links are present on the header I want to capture them and I want to find how many links are there in the header part how
41:32
Speaker A
many links are there in the header part I want to just find out so what we can do is we need to find out one common thing between all all of them so if I inspect somewhere on the header just
41:46
Speaker A
observe or let us take one element let's take a four number like this and uh you can see this so what is this here DMD in L this is the class attribute of this for number and if I look at here
42:08
Speaker A
there is a li tag and ul ul is representing user list and Li is representing list item so here if I look at the HTML every Li tag is representing one element on the header if I look at here see every
42:24
Speaker A
La how many l are here 1 2 3 4 five L are there these five L are representing five elements on the header you can just clearly see that whenever I'm moving the cursor on one Li to another Li it
42:37
Speaker A
is highlighting the particular element on the web page you can see that so now I want to find how many header links are there on the web page so I want to capture all of them how to capture we need
42:51
Speaker A
to find one common thing what is the common thing what is a common attribute between them here you can see the class you can see there is a class attribute is there for Li tag okay so list in
43:02
Speaker A
line item and again if I use Li tag which is which is also common right but it will able to get all other links also maybe some other Li items in the bottom list there are so many links are there even
43:15
Speaker A
this also we will able to get but I don't want this fooder links I want only header links so that is the reason I'm choosing this one so this is a UL which is highlighting the entire part which the each and every list item so now I want to use this class attribute value which will able
43:34
Speaker A
to match these five elements okay so what we can do now let us try to capture this class because the value of the class attribute is same for all these elements now observe very carefully this is something new you will learn see here we are using class attribute which is common for all the
43:58
Speaker A
elements and also second thing is what we are able to we are trying to capture multiple web elements with that class attribute we are trying to capture multiple web elements not a single web element so whenever you want to capture multiple web elements you have to use find elements this is the method
44:23
Speaker A
okay whenever you want to capture one single web element you have to use find element when you want to capture multiple elements or group of web elements you have to use find elements so here we have to use by do class name here we specify the class name and semicolon so find element method
44:51
Speaker A
will return one single web element find elements method will return multiple web elements multiple web elements now what is a written type of here I'm just keeping one variable here header links now what is the type of this variable if it is a find element obviously this variable type is web
45:18
Speaker A
element but here we are using find elements this will return multiple web elements so if you want to store multiple things into a variable we have to go with either list collection or set collection and what type of elements it is storing web element so the variable should be list of web
45:46
Speaker A
element okay now we need to import the list collection from java.util and also we have to import web element so the written type of find elements is what list of web element if you put the cursor it will show you the list of web element is a written type of find elements
46:08
Speaker A
list is a what Java collection list is what Java collection okay so my question is why they make it as a list why can't it is a set so whatever return type they are mentioning same return type we have to specify here we cannot say set here they're returning list collection so the variable
46:31
Speaker A
should be list we cannot use list here why they make as a list why they can't make it as a set what could be the reason for that yes whenever you are capturing the elements group of elements there
46:46
Speaker A
may be some duplicates also available right there is a chance of having group duplicate elements so if it is a set if it is a list collection it is is allowed to have duplicates but if it is a set
47:00
Speaker A
collection it cannot have duplicates so that is the reason they have designed this find elements method return return type is list of web element remember this point find element is a method which will return return single web element so the type of variable should be web element find elements
47:20
Speaker A
method will return return group of web elements so the return type will be list means which can have multiple and every value is what web element type web element type so this is the return type of find elements okay now I captured all the header links in this particular variable can
47:43
Speaker A
we find how many are capture how many header links are there total number of header links how to how to count yes so suppose if you have some values or elements in the list collection how can we find size of the collection size of the list by using size method right size is a
48:06
Speaker A
method which is available for list collection in Java sessions we discussed about this how to find total number of elements in the list by using size method so I can say header links do size so this will return return total number of elements now I'm just printing here so here I can
48:28
Speaker A
say total number of header links header links I'm just printing that value and later if you want to use it for validation you can use it so this is a scenario why we need to capture group of elements
48:46
Speaker A
and why we need to use a class name because all these group of elements are having same class name attribute so same value for class name so I'm using that attribute in the class and which will able to capture multiple elements or group of web elements then we can find total number
49:02
Speaker A
of links like this F so let's try to execute now this is one scenario if you want to capture group of elements if they are having same value for class attribute we can use class name okay now I can just go back yeah now it is saying seven so why it is seven here we have only 1 2 3 4 5 right
49:27
Speaker A
why it is seven because there are some other hidden links also there even this this is also captured the arrow and uh the symbols are there right here and they also captured so some hidden uh header links also there they are also having same class name sometimes they may not able to
49:47
Speaker A
visible on the page but if you open this page on the mobile device Sometimes some elements will be displayed so these are the header links which is captured from the web page yes we can also print each and every Link in the console window and also we can perform click action on every link
50:07
Speaker A
and we can also verify the target page so that in all Next Level okay how to validate the links I will show you that again different concept I'm not introducing that here so what is link how many types of links what is broken link how to find it this is all different concept here our
50:24
Speaker A
focus is only on the locators concept okay don't go beyond that so we will cover that part in the next sessions so here just trick to only locators concept don't go beyond that so how to use CSS how to use class name here to locate group of web elements is this clear everyone how to use class
50:45
Speaker A
name locator to locate group of web elements yes so now let me try to show you one more example for example tag name we also have tag name this is for class class name so now let us see how
51:01
Speaker A
to use Tag name for getting group of web elements sometimes uh we want to capture group of elements and you don't see class attribute for them so we need to find some alternative so we can use Tag name also to locate a group of web elements we can also use Tag name for locating group of we
51:23
Speaker A
elment for example on this particular page there are so many number of uh uh links are present so many links are present in this particular page so now I want to find all the links including header links this menu links and your product links and in the bottom of the page also there are so many
51:44
Speaker A
links are there so I want to find out how many total number of links present on this particular web page okay so what we need to do is we need to find one common thing in all the links so what is
51:56
Speaker A
a common thing which we have in the links is tag name so if you inspect any link the tag name will be the same that is a a is always representing the tag name anchor tag even if you inspect any
52:11
Speaker A
other bottom link for example here about us the anchor tag is there so for every link there is a same tag name which is a so with this we will able to capture all the links from the web page so now
52:25
Speaker A
let me show you how to use Tag name as a locator to get group of web elements again driver dot which one we have to use find element or find elements I want to get multiple web elements obviously we use find elements find Elements by dot tag Name by dot tag name so what is the tag
52:49
Speaker A
name you're looking for a a is a tag P that's it end the statement so this will also return all the links in the form of web elements so what is a return type here return type I'm just keeping
53:05
Speaker A
links variable here return return type is what again list of web element this is a return type so now I captured all the links into variable now the next thing is what we want to find how many links are there so how can we find total number of links total number number of links total number
53:30
Speaker A
of links links dot size links dot size because it is a collection we can use size so this will return total number of links which are present on this particular page all the links on the web page okay now can just go back now totally 76 links are there in the web page so according to
53:58
Speaker A
your requirement what you are expecting suppose you expecting 100 links but here only 76 links are there so after comparison you can make our test failed and if the links count is same you can make our test passed so like this we can put some validations on this values similarly
54:15
Speaker A
let me show you something else so by using tag name also we can capture group of elements now my requirement is I want to find total number of images on this web page how many total number of images are there images in the sense the slider images the product images here the logo images
54:35
Speaker A
all images how many total number of images are there in this web page I want to find out so what is the common thing which we have in these images is again tag name so tag name is always
54:48
Speaker A
IMG if you inspect any image the tag name of the image will be IMG wherever you see IMG you can simply find that is a image element okay so now I want to capture all the images which are
55:03
Speaker A
present on the web page so what we can do again we can use same tag name locator say driver dot find Elements by dot tag name tag name is what IMG so this will return all the images on the
55:22
Speaker A
web page and again return type is what list of web element is a read type now if you want to find total number of images you can simply print here total number of total number of images is what images do size so this will give you total number of images present on the web
55:48
Speaker A
page okay so this is how we can simply use Tag name you get a group of elements so tag name and class name so these two locators especially we use for getting group of elements or capturing group of web elements and ID name link test partial link test we can use to get specific
56:13
Speaker A
type of web elements okay you can also print the text uh text with size text with the size means what not understood so size in the sense once you capture all the web elements in a variable we can find
56:39
Speaker A
the size means which will return total number of images in this particular collection in case of links we already captured all the links here so here we find out a total number of links and again if you want to print those link test you can print again we have to write another
56:58
Speaker A
for Loop and capture link text from every link and you can print that link text in the console window okay that is again different validation and we will discuss these elements separately okay we will deal with all kinds of elements how to handle input boxes how to handle links how to
57:15
Speaker A
handle images so we'll individually discuss each and every web element and that time I will show you what are all validations we can do on the links how to capture links how to click on the links how to validate Target URLs okay so we'll discuss them in the separate session because here
57:32
Speaker A
the concept is what only locators so just stick to only locators concept okay so guys are you clear about these basic locators how to use ID name link text partial link text tag name and class name so now I'll discuss one important concept very very important inter question we already discussed but
57:57
Speaker A
we will go in detail in depth we understood two methods find element find elements two methods right so if anybody ask you what is the difference between find element and find elements most of the people 90% of the people will say find element will locate one single web element find elements
58:23
Speaker A
will locate group of web elements or multiple web element elements this is the only single answer people will say but there are other differences also there with this two methods so just common answer is what find element will return single web element and find elements method will return
58:45
Speaker A
multiple web elements so this is the common answer most of the people will tell so now let us try to differentiate these two methods in the different scenarios by taking different scenarios okay now my first scenario is this is most important listen very carefully first scenario so in
59:09
Speaker A
the find element in find elements in both the methods we have to pass locator right in both the methods we have to pass locator using buy. ID or buy. class or by. tag whatever both the methods will take locator as a parameter so in scenario one for example my locator whatever locator I'm
59:30
Speaker A
providing if the locator is matching with a single web element let us assume whatever locator I'm passing in this methods which is locating single web element the locator is exactly pointing single web element now I'm using that locator in find element method and also I'm using that locator
59:55
Speaker A
in find elements method observe very carefully so here the scenario is what locator is matching with the single web element on the web page now I'm using that locator in the find element what this will return return obviously this will return single web element single web element
60:17
Speaker A
right because the locator is pointing single web element and here outside also we use find element both are same so single web element will be return and what is a return type of this web element is a return return type return type is what web element this is more clear for
60:36
Speaker A
us locator is pointing with single web element find element also will able to return one single web element so ultimately we will get single web element the return type is web element now the locator is matching single web element but we are using in find elements instead of find element I'm
60:58
Speaker A
using find Elements which will able to locate group of elements but the locator is pointing single web element now tell me what this will return locator is pointing single web element but find elements method will able to return multiple elements but locator is pointing with a single web
61:19
Speaker A
element yes so in the list collection can we store single value list collection is meant for what to store group of values but still we can store one single value right in the list collection list contains a single value also right so find elements will return list
61:47
Speaker A
collection but because locator is pointing with a single web element only that element will be returned in the list collection so this will also return single web element but the return return type is what still it is a list still is a list but the list collection contains a
62:09
Speaker A
single web element if the locator is pointing to multiple elements the list collection contains a multiple web elements if the locator is pointing to single web element the list collection contains a single web element is this clear everyone so find element with locator find elements with
62:28
Speaker A
single locator so here locator is matching with single web element if it is a find element return type is web element if it is a find elements written type is still list of web element even though it is rning single web element return return type still it is list of element okay
62:49
Speaker A
this is the first scenario now the second scenario scenario two locator is matching with multiple web elements okay locator whatever it can be tag name or class name whatever so locator is matching with multiple web elements but I'm using find element of locator again find
63:19
Speaker A
elements of locator now tell me the first one this locator is matching with multiple web elements group of web elements but here I'm using only find element so what this will return single or multiple elements so here the locator is pointing group of elements but here
63:44
Speaker A
I'm using find element will this return group of elements or single web element listen very carefully locator is matching with group of web elements or multiple web elements but here find element is able to return only one element at a time find element cannot return return
64:02
Speaker A
multiple values but locator is pointing multiple elements then how it will return what this will return see very important scenario if the locator is pointing multiple web elements let us say here I have multiple web elements first element second third four five element
64:26
Speaker A
elements are there and this locator is pointing all the elements but I'm using find element method this will able to return only single web element so even though this locator is matching with multiple web elements because we are using find element it will return first element on the
64:46
Speaker A
web page okay whichever element comes first on the HTML and that element will be return return ultimately the return return type is what again web element return type is web element got my point so even though that locator is matching with multiple web elements because we use find
65:07
Speaker A
element it will return first element on this list on multiple group of elements whichever element comes first on the web Bas only that element will be returned so this is the case right so now this is the second case in the second case locator is matching with multiple
65:29
Speaker A
web elements but when I use find element outside still it is rning single web element sorry single web element will be written and return return type is what return return type is still web element because whenever use find element that is always return web element now
65:55
Speaker A
comp the second case the locator is matching with multiple web elements and outside also I'm using find elements so what this will return single element or multiple web elements yes this will return multiple web elements because the locator is matching multiple web elements and outside also
66:15
Speaker A
we use find elements so obviously it will return multiple web elements and return type is what rest of web element this is a return type so scenario two is also clear now third scenario which is most important many interviews people will ask you this scenario scenario 3 suppose my locator is
66:44
Speaker A
not matching with any of the elements single or multiple locator is not matching with any element or elements singular or multiple whatever our locator is not matching with any of the elements on the web page now I'm using find element of locator find elements of locator now tell me what
67:13
Speaker A
the first one will return locator is not matching with any of the elements locator is not matching with any of the elements but still I'm using it in the find element what this will return this will return an exception what is that exception no such element exception no such
67:36
Speaker A
element exception will be returned okay if locator is not matching with any of the element on the web page if still use in find element so find element method will return no such element exception no such element exception will be returned if the locator is not matching with any of the element
68:00
Speaker A
now in case of find elements the locator is not matching with any of these elements locator is not matching with any of the elements on the web page now what find elements method will return return can you guess what find elements method will
68:20
Speaker A
return it will not throw any exception instead it will just return zero it will return zero elements okay because the locator is not matching with any of the elements obviously zero so find elements always returns what number of elements number of ele if no element found obviously the list
68:45
Speaker A
collection contains what zero so after using it if you find total number of elements it is returning zero it will not throw any exception this will not throw any exception the return return is zero so that is a major difference between find element and find element if the
69:06
Speaker A
locator is not matching with any of the element find element method will return no such element exception in case of find elements it will not throw any exception will not throw any exception it will when you count it it will just returns zero but it will not throw any
69:28
Speaker A
exception or find element method will throw some exception okay so is this clear everyone these are the three scenarios you need to remember when I use find element and find elements yes it will create an empty list collection if there is no element obviously
69:50
Speaker A
the list will create which is doesn't have anything no values in the list m list will be created okay so this is all about the basic type of locators we have discussed about ID name link text and partial link text class name and tag name and along with this we try to understand
70:17
Speaker A
what are the differences between find element and find elements and also we have discussed about what is the difference between link text and partial link text okay so we'll stop here for today's session in the tomorrow session we will discuss about CSS and expa so these two are very
70:39
Speaker A
huge Concepts we'll take another two sessions for CSS and xath and then we will finish the locators concept and this presentation I will share once we completed this entire topic so there are so many slides are there only related to locators all the locators have covered in this slide so I
70:57
Speaker A
will share this slide once we have completed this concept then you then you'll able to understand this okay yeah so I will stop here for today's session and uh come to the assignment part for today's assignment uh you guys can try this very simple based upon today's topics so this
71:15
Speaker A
is the application you can just open this is your application URL I think we used this yesterday also demo.com so in this particular page you have to find total number of links and also you can print them if you want to print you can print those total number of links okay so okay or
71:41
Speaker A
else you can just leave this for now in the next sessions I will tell you so total number of links and total number of images okay and also click on any product link using link text and product link
71:54
Speaker A
test you can choose any of the product and then click on this link like this using partial link text or link text just do one round of practice so whatever topics we discussed today based on the topics you guys can try this this is assignment for today and tomorrow we'll discuss uh other
72:13
Speaker A
type of locators okay so that's all for today's session I will stop here we'll continue tomorrow
Topics:SeleniumJavaLocatorsIDNameLinkTextPartialLinkTextClassNameTagNameAutomation Testing

Frequently Asked Questions

What are the basic locators supported by Selenium?

Selenium supports basic locators such as ID, Name, LinkText, PartialLinkText, ClassName, and TagName, which are directly accessible via the By class.

Why do we need customized locators in Selenium?

Customized locators like XPath and CSS selectors are needed when basic locators are insufficient or when element attributes change dynamically, allowing more precise and flexible element identification.

How important is HTML knowledge for using Selenium locators?

Understanding HTML tags, attributes, and values is crucial for creating effective locators, as it helps identify the unique properties of web elements needed for automation.

Get More with the Söz AI App

Transcribe recordings, audio files, and YouTube videos — with AI summaries, speaker detection, and unlimited transcriptions.

Or transcribe another YouTube video here →