How to Retrieve a Collection of WebElements?

Dezlearn Education
A free video tutorial from Dezlearn Education
Learn IT Easy
4.4 instructor rating • 7 courses • 68,661 students

Learn more from the full course

The Complete Selenium WebDriver with Java Course

Learn Selenium WebDriver with Java, TestNG Framework, Page Object Model, Page Factory Design. Learn Coding From Scratch.

20:21:42 of on-demand video • Updated January 2021

  • Core Java Programming and Selenium WebDriver Automation
  • New to coding? No worries. This course covers coding from scratch
  • Ideal for professionals transitioning to DevOps wanting to get a coding booster
  • Perfect course for manual testers aspiring to switch into automation world
  • Creating Automation Framework from Scratch using Page Object Model and Page Factory
  • Selenium WebDriver Programing from Basic to Advance
  • Object Oriented Programing (OOPS)
  • Data Structures in Java
  • TestNG Framework
  • Regular Expressions in Java
  • Element Locating Strategies
  • Exception Handling in Java
  • Working with File System in Java
  • Advanced Java Programming
  • Selenium WebDriver Basics
  • Date and Time Operations (Rev. in Java 8)
  • Locating Elements using XPath
  • Locating Elements using CSS Selectors
  • Working with Element Collections
  • Web Page Data Extraction
  • Automating DatePickers, WebTables
  • Page Object Model
  • Automating Browser Popups
  • Automating iFrames
  • Automating JavaScript Alerts
  • Automating Mouse Actions
  • Test Synchronization
  • Instructor Guidance and Query Resolution Support
  • No PPTs. Programming Oriented Sessions.
English Hello everyone.. In this section of videos we're going to understand how to deal with the collection of elements.. So not just one element but the collection of elements. How we can retrieve it. How we can take the collection into the list and how to work upon it. So this is very important as far as the automation is concerned. We often need to deal with more than one elements of similar type and whatever we're going to see in this particular section of videos maybe three to four sessions, we'll understand how to retrieve the collection and how to apply different methods on that. So let's understand these concepts by using simple examples. In this session let us see how to take the collection of all the links from a web page. So for example we go to a sample page, actitime.com and on this particular page let's see how to get the collection of all the links from this page. So when I select a CSS over here and if I just use the tag name.. You know in CSS we can just use a tag name and It'll become a CSS Selector. So in this case if we just use 'a' that is anchor that's for a links, I see there are 75 links in this particular page and these are all highlighted over here. Now let's see how we can get the collection and also when we retrieve these links the 75 links.. Let's also try to print the name of the link, if the name is present because sometimes a link is there but it might be a logo or it might be an image.. So it might not have actual text for the link but let's see let's get the collection of all these 75 links and let's print the text associated with that link if there is any, let's go to eclipse over here I've already set the properties for Chrome driver executable and launching the instance of the chrome driver by using the driver, let's use get method to go to this actitime page, and then by using driver.findElement, now in this case we'll not use find element, find element as we saw so far, we'll give us only one element but when we want a collection, a list return then in that case we need to use the find elements.. So list find elements and over here by in this case we use CSS Selector that is simple a and when we retrieve these elements, we need to keep them inside some list. So let's create a list object here list of what list of web elements and let's give it some name, let's say (links =) so we created a list object that is links, this is the list of web element, which is going to be captured by the driver.. Now in this case we need to import the list from Java.util and also we need to import the web element from selenium and that's it. Now this particular links, the list will contain all the web elements of 'a' category in this case links. We can use a modified for loop over here, let's say for every link in the collection of links you know if you're new to these lists and this over all concept of collections from Java point of view, I would suggest you to go to the Java tutorial and refer all these videos, so these concepts like using the modified for loop, using the conventional for loop etc.. etc.. will get clear. So over here we're going to use this modified or special for loop where we'll use the concept of for each object in the collection of objects, so in this case for each link in the collection of links. What is this type. It's web elements, so we'll see for each web element, let's call it as a link in the collection of links. So now this for loop will iterate from the very first web element to the very last inside this particular list, now when we iterate what we'll do we'll capture the text and then we'll print that text in the console. So the way we do it is by using simple [CODE] So It'll get us the text of the link, if it is present and then simply after that I'm going to add a dotted line just to get a better visual in the output.. So before we run the program, let me just do driver.close.. So It'll closed the driver. So let's run it and see how we can retrieve the links and see the text of each link. So I'm not going to open the browser, you can stick to the eclipse over here and as we can see the names of the links. So these empty spots whatever you see. These are the links but they do not have any name.. So these are all the name of the links. If that is there, now what if I want to modify this program a little bit and want to print the name only if it is present.. If the link does not have any name, it might be a image or a logo or something like that which doesn't actually have text. So how we can modify it. We can simply do something like this. Let's take a string let's say inside the string we're going to take the linked text. And over here I'm going to simply put one if condition, [CODE] So if it is not empty then only go and do all these can we say that. What did we do first. We first took the text of the link inside a string and then we're applying if condition on that we're saying that if this text is not empty then only go inside and print it. If we run the program again, all those links which does not have any text they'll be filtered out. So over here you see all the links with the text.. Right! So apart from text can we get any other attribute. Yes. Let's go to the page and let's see a link. Let's say for a link we've the class attribute. Also we've href that is the actual URL where link is pointing to.. But let's say, let's use the class attribute. Let's see what is the class for each link to retrieve the value of the attribute or basically to retrieve the value for any of the attributes for a particular element, we can use get attribute that is something called as [CODE] and then provide the name of the attribute. So in this case class, so what It'll do is from the particular link It'll fetch the value of the class attribute. What if for a particular element class attribute is not present. It's okay, It'll not fetch anything.. It'll just be blank but if class attribute is present, so It'll fetch the value of that particular attribute.. So in this case when we run the program again along with the text of the link also we can fetch the class of the link also.. So over you'll see this first is the text. Second is the class value and then depending upon the program we can do the validation like, this link should be always present, that link should be always present and this link should not be duplicate and all.. When we've a collection of links then depending upon whatever validation that we want to do, we can do it. So this is a very simple example of working with the collection of elements, how we can retrieve the collection and how we can take it inside a list object and how we can iterate over it and fetch the values and do certain validations. Now what elements will be fetched is totally depend upon what selectors we use like doesn't matter. We can use a CSS Selector or we can use a XPath or we can use the basic locators using ID or tag name or class or name etc.. That doesn't matter but what matters is whatever locators or selectors that we're using. What bunch of elements it can represent like in this case we saw this, CSS is representing 75 elements.. So that's why we could get those 75 elements inside this list object and then work with those.. Let's say I'm not interested in all these links, let's just assume that we're not interested to fetch all these 75 links on this page, but we're just interested in taking the collection of all these menu items.. So we want to take the collection of all the main menu items over here not even sub menu, only this main menu and then get the collection and iterate over that and do certain validations later on.. So how we can modify the CSS.. The logic is first inspect. Now we want to take this menu, so let's inspect that. So when we inspect the menu we see this as a link fine. It has got a class attribute and which says main menu link. OK so this can be useful.. Now it changes from application to application. What I'm trying to tell you is how we can proceed in such a case. The way it to proceed is we'll first inspect. Now let's inspect the second menu just to find out what are the common things.. So when you inspect the second menu. This is also a tag fine. It have also got a class. So what I can assume is all these menu items are links and they've a class attribute in common which is main menu link. So what will happen if I tried to modify the CSS to something like (a.) this class value.. Now what we see here is exact 6 matches, so we got a CSS Selector which represents exact this 6 menu items.. Now when we use this CSS over here, How many elements it is going to written.. Just 6.. So this links list object will contain only 6 elements and then when we run the program we're getting all those main menu items. Now a test case could be something like run a test on this particular page and we should see these 6 menu items always. Now once we get all the menu items in the list, we can run a loop like this and then we can add our assertion over there like this should be present and that should be present etc.. but the main thing is getting this CSS Selector or anything XPath or whatever you like. Which can give us those exact 6 items. So this is a very basic, nice example of how we can retrieve the collection of elements and how we can work with them.. In the next session we'll take another example, just to explore the concept. Thank you..