And look at how to choose and download files from a sequence of drop-down lists so let's get started to get started i've created a new excel workbook saved it as a macro enabled file and in the visual basic editor i've created a new module and i've already set a reference to the selenium type library so if i head to tools and references we'll see we've got a reference to the selenium type library set so i'm assuming you already know at least the basics of working with selenium at least how to get it set up and if not then we do have a separate playlist which explains how to get.
Started with that the first example for this video we're going to be using amazon.co.uk and we're going to be selecting a category of item to search for from this drop-down list now the really important thing about this particular example is that this drop-down list is a true select element if i right click on that drop-down list and choose to inspect it if i look over at the elements panel i have indeed selected a select element and this select element has multiple option elements stored inside.
It which provides access to the individual things we can select now the reason i'm making such a big deal about mentioning this is that you will encounter on various websites things which look like select elements but actually aren't you'll find many examples of these if i head over to the olympic medal count table for example if i right click on the all sports drop down list which i agree looks like a drop down list if i choose to inspect that that definitely is not a select element you.
Can see that that's just a button element this one has an unordered list inside it with the various options to select so these are individual list elements or li elements you'll see other examples there's a coronavirus um our world in data page again another drop down list of what looks like a select element if i right click on it and choose to inspect it this is just a heavily stylized div element and again many other examples of that sort of thing across a variety of.
Different websites things which look like select elements but actually aren't and the reason i mention that is because this video is focusing just on select elements there are some things we can do in selenium with a select element that don't apply to these other heavily stylized things which look like select elements so having gotten that out of the way with the next thing we need to do is create the basic code to open up this particular page in a new instance of google chrome so i'm just going to copy this url from the.
And then we can say cd.start and then cd.get and then in some double quotes paste in that url i've just copied and having done that basic thing we'll just run that subroutine to make sure it fires up a new instance of chrome and navigates to the amazon.com uk homepage next i'm going to add a bit of basic code to click on the accept cookies button it's not particularly important for this example to do that we can happily interact with the select element while leaving the cookie's message displayed.
I just find it annoying so i'm going to click on the accept cookies button if i right click on it and choose to inspect that button we should find i might need to do that again right click and inspect there we go that button's got a specific id so you can find in the elements page or list there's an input element with an id attribute so if i double click on the id attribute and copy that to the clipboard i can then head back to the visual basic editor and i can say cd.find element by css to specify you're looking for an id with.
A css selector you begin with a hash symbol or a pound symbol paste that in the id that we copied close the double quotes close the round brackets and then apply the click method to that so having done that if we run the subroutine again our new instance of chrome will load amazon and then click on the accept cookies button next we can get a reference to this select element and all of its options and to begin with there's nothing special really that we need to do here we can treat this select element just like any other web element in selenium.
So of course i'll need to know how to reference it so let's right click on it and choose to inspect it i'll just do that again so i should get to the part that i want there we go there's my select element and there are various ways i could try to reference this object but the most convenient way for me is to refer to its id attribute so there's an id attribute whose value is a search drop down box so let's double click on that copy that to the clipboard and then take it back to the visual basic editor so i'm going to begin by declaring a variable at the top of the subroutine.
Which will hold a reference to a generic web element so let's call this one ddl as selenium dot web element then after i've clicked on the accept cookies button we can say set ddl equals cd.find element by css once again we're using an id so we're going to use a hash mark at the beginning of the the name paste in the id value we've copied close a double quotes and then close the round brackets.
To get a reference to all of the options in that drop-down list we can declare a new variable to hold a reference to a
Set of web elements so let's say dim which we call these opts or ops let's call them ops as a selenium dot web element so multiple items are collection of items and then after we've captured a reference to the drop down list we could say set ops equals ddl dot find elements by css.And then this time we're just going to use the option tag name so in the double quotes here we just write the word option close the double quotes and then close around brackets so the word option there refers to the tag name of the items we're trying to get a reference to once we've done that we could work with that collection in any fairly standard way maybe a simple thing to do would be to loop through the options we've selected so to do that i guess we'll need another variable so let's say dim.
Op as a selenium dot web element and then we can write a symbol for each loop so we could say for each op in ops op and then we can print out a couple of hopefully useful bits of information let's say debug dot print and we'll say op dot text comma op dot value okay having done all that let's run the subroutine and we'll get our basic list of options printed out into the immediate window when the.
Subroutine is finished so there we go those are all of the options we could have selected from that drop down list on amazon now selenium also provides us with a specific class for working with select elements which makes it slightly easier to work with them particularly when it comes to selecting things from the list so this time rather than declaring our ddl variable as a web element class let's declare it as a select element class what we'll then need to do is modify the.
Method we're using to capture a reference to that the find element by css method returns a reference as a web element and now a variable is no longer of that type so to make sure it's returned as a select element we can add to the end of this method the as select method having done that we can tidy up a little bit of the the other code we've written so we don't any longer really need this ops variable let's get rid of the web elements ops variable entirely.
And then let's get rid of the line which um set a reference in the ops variable and then in our for each loop we can say for each op in ddl dot options so this specific property of this select element class returns the list of all the options in it so the end result of this will be the same of course if i just clear the contents of the media window and then run the subroutine again i'll find i get my amazon page popping up.
And we'll find that we get the same list of options but in a slightly more succinct neater way there are a few other potentially useful properties you can use to find out information about the drop-down list and the options in it just to demonstrate a few of those quickly let's comment out the for each loop and i'm going to add a new debug.print statement which says debug.print and then the word count after a comma then i want to find out how many items belong to the dropdown list so i can say ddl.options the options property just returns a reference to an object of the web elements class if i.
Press ctrl and i after the word options you can see it's returning references of web elements so the web elements class has a count property which simply tells you how many things there are if i wanted to maybe find out what the selected item was i could say debug.print and then in some quotes there say select an item and then after a comma i can say ddl dot and then i can refer to the selected option property now the selected option property returns.
A reference to a web element object so again you can see in the tooltip if i press ctrl and i web element so i can print out things like it's text and also its value so let's copy that again at the end and say dot value this time instead just like any other web elements collection you can refer to the first and the last items so i said debug.print and i say uh the first in fact let's go with the last one i mean the first one will just be the selected item so let's.
Say last followed by a comma and then ddl.options.last
Now this method if i press ctrl and i at the end of that returns a reference to a web element so again i can refer to its text and its value in much the same way as i did with the selected option property and then let's just copy and paste that so i can change that to value okay so with just those few things done let's clear the contents of the immediate window and then just run the subroutine to find out a bit of extra information.About the drop-down list and the things in it and those are the bits of information that we get printed out probably the most important thing you'll want to do with the select element is of course select something from it and you've got three methods you can use to do that let's demonstrate these quickly if i just clear the debug.print statements and then say ddl dot select we've got three methods there select by index by text and by value the select by index method works only if.
The individual options within the select element have an index attribute so i just have a look back at my amazon list and select this um inspect this drop down list or select element we'll find that if we open these up and have a look at the individual option elements within there none of these have an index attribute they've got a value attribute and then they've got a bit of text but no index so we can't use the select by index method in this particular case.
But we could use the select by value option let's say we wanted to look for let's go for books i'm going to double click on the value attribute search alias strip books copy that to the clipboard head back to the visual basic editor and then say select by value and then in some double quotes paste in what i've just copied okay so having done that we could run the subroutine and we'll find that we load up amazon again and we get the books option selected in the list.
Rather than selecting by value of course we could have used the text displayed so if i just head back to the visual basic editor and then say ddl dot select sorry ddl dot select by text and this is nice and easy it's just the word books it's what's actually displayed to us i'll comment out the select by value method run the subroutine again and we'll see the same option get selected this time using the text rather than the value.
So just to wrap up this first example i suppose we'll actually want to search for something so having selected a category from the list we could then type something into this text box so let's find out what that is let's inspect it and then we can find that it has a unique id called to tab search text box so let's double click on that and copy that to the clipboard and then if we head back to the visual basic editor after we've selected our item we could say something like cd dot find element by css.
And then in some round brackets and double quotes type in a hash symbol and then paste in the id that we've copied close the double quotes on the round brackets and then apply the send keys method to that to type something into the box what should we say let's throw books on owls so i'll send keys owl to that um that text box next i want to find out what the name of the button is that i can click on so let's head back to the open instance of chrome and then find this search button.
Right click and inspect it and that also has a unique id it's called nav search submit button so let's double click on that to copy the id head back to the visual basic editor cd dot find element by css open some round brackets and quotes a hash mark paste in oh i failed to copy it sorry about that let's head back there try that again copy nav search submit button try that again there we go and then this time we can apply the click.
Method to that okay so once we've done that let's just quickly run the subroutine and find out what happens so we select books type in owl and click the search button and get a whole list of our books returned to us so in order to make this example at least vaguely useful i suppose we should write out our list of results somewhere in excel i'm not going to spend a huge amount of time doing this we've covered extracting results into worksheets and formatting.
Them in earlier videos so here we'll just take a quick and simple approach to write out the details into a new worksheet i need to right click i need to find out how to reference each of the individual elements here i suppose so let's right click on any one of these at random i'm going to right click and do to inspect it might actually help to maximize the window as well so we can see what's happening let me just try to do that again right click and inspect and let's find out the uh the main container for each individual item so just before we move on to the next item above okay so it looks like we have.
A variety of ways we could potentially refer to these objects we have an attribute called data component type with s search result as its value we've also got a class quite a few compound classes in fact s result item that sounds promising actually i'm going to go with the class s dash result dash item i'm going to copy that to the clipboard and then i'm going to head back to the visual basic editor and then let's declare a variable to hold the list of search results.
So we'll say dim which we call this srs search results as selenium dot web elements then after we've clicked our button we can say set srs equals cd dot find elements by css and then in some round brackets and some double quotes to search for a class we precede that with a full stop and then paste in the class name s-result dash item close the double quotes and close around brackets.
And then let's just find out how many results we've returned first of all let's make sure we are indeed actually finding something so we can say debug.print srs.count the exact same property we used with the options property from the select element earlier so if i just clear the contents of the immediate window and then run that subroutine again hopefully we will find that we return at least some items 26 of them in fact excellent so if we've.
Done that we could just write the results out into a new excel worksheet so to make that work we're going to say 2 srs.txt.2xl and then we're going to create a brand new worksheet in this workbook so we'll say this workbook.worksheets and then we're going to add a worksheet that returns a reference to the worksheet that's been added so in there i'm going to then reference range a1 on that worksheet and run the subroutine one more time and when it has finished this time if we.
Switch back into excel we should find a not particularly nicely formatted set of results but a set of results nonetheless so dealing with a single drop down list is fairly straightforward this select element class makes our life pretty easy but of course things aren't always going to be quite so simple for the next example we're going to consider a sequence of cascading drop down lists on the nvidia download drivers page.
Now this example does require a slight suspension of disbelief first of all you have to imagine that you've actually managed to find a graphics card available to buy and second of all you have to imagine that you're rich enough to afford the extortionate prices they're charging these days or perhaps that's just me getting old anyway imagine that happy situation you found a card and you can afford it and you've bought it and now you want to download the drivers for it so what we need to do is go through this sequence of cascading drop-down lists.
To find the product we've actually bought so in this example selecting an option from the first list affects the options available in the second and then selecting an option in the second list affects the options available in the third so i'd like to be able to go through that sequence of cascading lists to find my product so let's write the basic code to load this page and get a reference to that first drop-down list i'll just copy the address to the clipboard while i'm here then head back to the visual basic editor and let's create a new subroutine i'll.
Add this one to the top of my module i'll call this one sub nvidia select then inside there i'm going to paste in my url as a comment temporarily and then we can copy and paste a bunch of code from the amazon select example so let's just copy the three variable declarations and the start and get method lines then we can replace the url by cutting this one and pasting that in place of the amazon url get rid of that extra comment then we don't need this srs variable.
Let's get rid of that entirely we're going to have multiple drop down lists and multiple options to work through in this example so let's just inventively change the name of our variables to ddl1 and op1 that gives you a clue as to what my other inventive variable names might be later on and then having loaded the page let's get a reference to the first drop down list if i head back to the page and then right click on that drop-down list choose to inspect it i'll find that it's a select element.
With a unique id of cell product series type so let's double click on that and copy that to the clipboard head back to the visual basic editor and say set ddl1 equals cd.find element by css and in some round brackets and double quotes type in a hash mark and then paste in that id close the double quotes and then close around brackets and then don't forget this find element by css method returns a reference to a.
Web element object we're trying to capture this in a variable whose type is select element so don't forget the dot as select method at the end of that line having done that we should then simply be able to loop through the options of that drop down list so we could say for each op one in ddl wonder options say next op1 and then some simple debug.printing of op1.txt comma op1.value.
And having done that we can run the subroutine to give it a quick test and once it's finished eventually we should see the list of options appears in the immediate window so now that we know which options are available in that first drop down list it would be fairly easy to select one of those single options using either the text or the value what i'd really like to do is automatically select each one of these options in turn and then list out the options available in the second drop down list so let's add a little bit more code to.
Do that i'm going to copy my ddl1 variable and i'll just do this on the same line if i type in a comma at the end of the first variable declaration and then just paste that in and change its name to ddl2 then again i'll do a similar thing with the op1 variable add a second declaration on the same line and call that one op2 now we need to capture a reference to the ddl2 drop down list so let's copy and paste.
The set ddl1 and then change the name to set ddl2 then we need to find out what the name or the id of that second drop-down list is so let's inspect it and find out it's got a unique id of cell product series so we can copy that to the clipboard head back to the visual basic editor and then paste that in in the second set statement there so what we then want to do rather than just debug.printing options from the option.
One list or the drop down this one we'll still carry on printing them out because i think it's useful information to have but we also then want to select one of those options and to do this i'm going to say ddl1 dot select by let's go for select by text i'm going to use the op 1 dot text property to set that value so once i've done that i can then say for each if i can spell each there we go each op 2 in ddl2 dot.
Options next op2 and then debug.print up to dot text comma up to dot value i don't think the value is quite that important in this example and just to indicate that these are options of a previous list i'd also like to indent these in the immediate window by one tab space a quick and simple way to do that is just stick a comma at the beginning of the debug.print argument so having done that if i clear the contents of the immediate window.
Run the subroutine again we should actually physically see this selecting the individual options in the first drop-down list so titan g-force etc etc and once that has finished eventually there we go if we head back to the visual basic editor have a look in the immediate window we've got a series of options or sub options listed underneath the options from the main first drop down list.
The next step is fairly similar to what we've just done now we know which options are available in the second drop-down list we can pick each one in turn and then list out the values in the third drop-down list there is one small potential complication here to do with one of the options available if i have the nvidia rtx quadro series or product type selected the product series drop-down list has an option called show all product series now this doesn't affect the options available in the product drop-down list this affects the options available in.
The same drop-down list so clicking show or product series changes the list of options available now that could cause a problem if i'm already looping through that list so to avoid that potential problem we're going to avoid selecting that last option in the second drop down list there are several ways we could do this but i'm going to do this based on checking either the value or the text of that final option so back in the visual basic editor if i scroll down through the immediate window towards the end of the rtx section we'll find that i can either.