Excel VBA Introduction Part 57.3 - Using Different Web Browsers with Selenium

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to this ysl vba tutorial in this video we'll explain how you can use different web browsers with selenium we'll start with a quick reminder of how to open up google chrome and navigate to a website and then show you how to do the same thing using both the opera browser and the microsoft edge browser we'll briefly explain why the firefox browser sadly no longer works with selenium basic and then towards the end of the video explain how you can use the webdriver interface so that you can select the browser you want to use at runtime so let's get started here's the basic example we'll create in this video the idea is that i can select the name of a web browser from this drop-down list and then when i click the wiki me button it's going to generate a random article from wikipedia i genuinely have no idea what article i'm going to get by the way but i do know which which browser we're going to be using because i selected that from the drop-down list i'm not even going to attempt to pronounce that um a german scouting organizations great so if i wanted to join uh the scouts in germany now i guess i know how to do that perfect right let's try using a different browser i'll go for edge this time and then if i click wikime again it's going to change the browser of course so i'm using microsoft edge now and then i'll get something about valentine pier i have no idea where that is vancouver excellent and i can read all about that and find out everything there is to know about it um of course the point of the um the video isn't so much what page you generate it's about making sure you get the browser you want if you wanted to follow along with the video i'm going to make the assumption that you've already installed selenium basic and the driver for google chrome already if you haven't done that we do have a video which explains how to do that it's a fairly short one it's a fairly quick and easy setup to get selenium basic working on your machine and you can use this video to help you get that done let's start by writing some basic code to open up a web page using google chrome as we should already have that web driver installed i've got a brand new blank excel workbook over here i'll head up to the developer tab and open up the visual basic editor i need to set a reference to the selenium type library so i can head to the tools menu and choose references and then in the list i can find selenium type library and check the box next to that once you've done that you can click ok and then i'll insert a new module in the usual way and i'll declare a variable at the module level to hold a reference to the browser we're going to create i could declare the variable inside the subroutine but annoyingly that means that when the variable goes out of scope at the end of the subroutine it immediately closes down the browser so if i declare a private variable i'll call it br as selenium.chromedriver as we know we're going to open up google chrome first then i can create a subroutine uh let's call it something like use different browsers and then in here we can set a reference to a to a new instance of a chrome driver so we can say set br equals new selenium dot chrome driver now we can start the browser by saying br.start and then we can get a web page by using the get method so we can say br.get and then pass in the url that we want to navigate to i'll just type in a couple of double quotes there before we fill in the actual url now wikipedia has a facility which allows you to generate a random page if i head back to my open instance of chrome and head over to this page here it describes how the random page feature works the reason i've gone here is so that i can just quickly copy and paste the um the url from this link here so where it's a special colon random i can right click and choose copy link address and then i can head back to my vb code and paste that address in inside the double quotes so having done that it's probably worthwhile giving it a quick simple check so we can run that subroutine and we'll open up a new instance of chrome and it should generate a random article from wikipedia there we go now in order for that to work of course we need to have the chrome browser installed on the computer but we also need to make sure that the chrome driver is installed in the correct location so as we described in the earlier video which shows you how to get set up to use google chrome in selenium basic i've got my chrome driver file installed in the selenium basic installation folder if we want to be able to use a different browser then we need to make sure we have the correct driver files installed in this same location and good way to find out which driver files you'll need by the way is to head over to the official documentation for selenium so i'll post a link to this page in the description below the video and you'll find it in the original video anyway as well so let's say we wanted to use opera the new version of opera the the chromium-based opera browser if i wanted to get access to the web driver for that i can simply follow this link from this page and that takes me to a page on github i've got a list of releases here on the right-hand side so i can click on the latest release and then i can find the link to install the opera driver or download the opera driver for the correct version of windows that i'm using so i'll go with the win64 version here i'll just click that link to download the file shouldn't take too long it's not particularly large when it has downloaded i can choose to open that in its download location so i can click on the arrow there and choose to show this file in the folder i can then right click on it and choose to extract it and then click extract and then i can double click on the extracted folder and into that folder i can find the opera driver executable file copy that from that location head back to my selenium basic folder and then paste that straight in to the selenium basic installation okay now that i've done that i can head back to my vb code and then i can change the two references to a chrome driver to the opera driver instead so in the variable declaration at the top of the module and also in the line of code where we set the browser reference to be a new instance so i can set that to be a an opera driver as well having done that of course you'll need to make sure that you have opera installed on the computer i'm not going to take you through that part i'm sure you can work out how to do that part yourself um but as soon as i've changed all that i can run that subroutine again and this time it will open up opera instead and give me a completely different random article now let's do the same thing for microsoft edge to do that i can head back to the browsers page in the selenium documentation find the link for edge and then open up that page you'll find a couple of options for edge there's the new chromium based version and the old legacy version i've got the new version installed you see i've just actually opened up a copy of edge um on my machine so this is the new version so i'm going to scroll down that column and then find the list of downloads for the new versions now it's a little bit more complicated here as you can tell there are lots of different releases for edge and the reason i opened up a copy of edge was so that i could find out which particular version i'm currently using so i can open up edge head to the options menu at the top right hand corner and then choose the help and feedback menu and then choose about microsoft edge and on the page that loads it tells me which version i'm currently using tells me it's up to date i'm not sure that's quite true there's a couple of versions been released since this but i'm using 87.0664 so i can then match that up with the version or the versions listed in the releases for microsoft edge so that's the one that i'm currently using and again i'm going to download the 64-bit version of that driver so i can click that link to download the file and again it won't take too long not particularly large file when it has downloaded i can view it in its folder and once again it looks as if i need to un unzip it or extract it so i can right click on it choose extract all and then extract and then i can double click into that folder and then find the ms edge driver i'll copy that to the same selenium basic folder and there's the edge driver installed okay so now let's give this edge driver a quick test let's head back to the visual basic editor and we can do the same thing we did earlier we can switch the current type of driver to an edge driver and then the same thing here as well and then when we run the subroutine sadly things don't work quite so smoothly this time we get a runtime error message complaining that it can't find the driver that we think we've just installed um slightly irritatingly this is due to the fact that microsoft seemed to have changed the name of the edge driver since selenium basic was released and selenium basic hasn't been maintained since 2016 i think and that was the last release that was uh that was created so at that point microsoft's edge driver was called edge driver but it's now called ms edge driver as you can see if i just end this subroutine here and head back to where we've just pasted in our ms edge driver um it's got the wrong name fortunately this is a fairly simple fix what we can do here is just rename that file to take away the ms at the beginning now it's called edgedriver and then if we switch back to the visual basic editor and then run that subroutine again everything's happy we get the new version of that you opened up and hopefully a new random page there we go at this point i should make a quick mention of firefox um if you scroll back to the page for the uh the browser's documentation on the selenium website there is indeed a link for the web driver for firefox however it doesn't work for selenium basic in vba as i mentioned selenium basic was last updated in 2016 and firefox has come on quite a long way since then and they've moved from the old driver they were using to a completely new version of that which simply doesn't work with selenium basic um you are more than welcome to give it a try please don't take my word for granted um don't trust me about anything i say if you wanted to download that driver you're more than welcome to so i can head over to the the geckodriver page on github and then find the latest release and then scroll down the page to find the gecko driver for windows 64-bit and again if i can download that that shouldn't take too long and then i can show that file in its folder and i can extract it and then i can find the gecko driver and copy that to the selenium basic installation folder okay now if i go back to my vb editor and change my edge driver to a firefox driver we can still write the code of course the uh the firefox driver class is still in the selenium type library but sadly this does not work you can see that it's searching for a completely different driver type altogether and it isn't sufficient just to change the name of the gecko driver it works in a fundamentally different way and if you were desperate to use firefox and i believe you can grab a very old version of that i think version 46 but i think that's probably going back a little bit too far to be to be able to do anything useful so we're not going to be using firefox as uh as it's not properly supported so i'm going to just delete the gecko driver that i've downloaded there and then i'm just going to tidy up a few more of these windows i can close some of these down so let's get rid of these as we shall need them for the next part of the video so at the moment we're relying on changing the class we're declaring our variable as to change between the different browsers but that's not going to be particularly useful if we wanted to create a system i showed you at the start of the video with a basic drop down list to switch between them there's a different way we can launch different browsers using selenium what i'm going to do here is change the class i'm declaring my variable as to something called the generic web driver and webdriver is actually an interface as i understand it an interface is almost you can always think of an interface as like a blueprint for a class so all of the specific browser drivers rely on the web driver so they all implement the interface in the same way they all use the exact same properties and methods so you can guarantee that when you declare a variable as a chrome driver or as an edge driver they inherit all the same methods and properties as the generic web driver so i'm going to change my declarations and instantiations to web drivers but that means that the code won't any longer work at this point if i try to run it it's going to say i haven't defined the browser because indeed i haven't i've created a most the most generic possible web driver that isn't specific to any one browser we can refer to or we can instantiate a specific browser in a couple of different ways but the way we're going to do it is by specifying the value of an optional parameter in the start method of the driver object so if i type in a space after the word start i expose this optional parameter called browser so for example nice and simply if i wanted it to be chrome i can type in chrome if i wanted it to be edge i could type in edge opera opera etc having done that if i run the subroutine again now we'll find that it opens up google chrome and will load eventually a completely random um wikipedia article so we can just carry on changing that bit of text to whichever browser we like and then we'll get the code using that browser so now that we've done that we can rather than typing in our web browser name manually get it to reference the value of a cell in an excel worksheet so this part's pretty easy really it's just using some basic excel techniques if i head back to my excel workbook i can pick any old cell that i like i'll type in choose browser in cell b2 and then just do some very basic formatting for that nothing particularly complicated or particularly attractive either um feel free to uh improve on that formatting um it's not difficult and then i'm going to use cell c2 to create a drop down list of browser names there are so many different ways we can do this i'm going to take quite a simple approach by using the data validation feature in excel so from the data tab in the ribbon i can head over to the validation tool so there's data validation sitting just there and then from the drop down list i can select the uh the data validation option or just click on the the button part of that tool and then i get to choose how to set up the allowed values for this cell so from the any value or the allow option i'm going to choose list and then in the source i can simply type in the list of browser names i would like to be able to use you can type this in a comma separated list do make sure you spell them properly of course we could also reference a list of cells in the worksheet if we have our browser names stored elsewhere but that's a nice simple example for this code so edge chrome opera click ok and now i can click on a drop down list to select the browser i want to use now we simply need to point our code to the value of the cell in which we've selected the browser name so we can head back to the visual basic editor and instead of specifying the browser name as a string literal we can point to a cell on sheet1 so i'm going to reference that worksheet by codename so i'll say sheet1 dot range c2 dot value we could add a check to make sure that the cell has a value you'd have a basic if statement there to check that it's not an empty string but i think this is sufficient for our basic example so having done that if i switch back to the workbook i'll head to the developer tab in the ribbon and then from the insert tool i'll insert a basic form control button to launch our uh code so i'm just going to draw a button in this cell if you wanted to make sure your button snaps to the edges of cells you can hold down the alt key as you draw the button and you'll find it snaps to the edges so that's quite satisfying when you can do that and then i can select the only subroutine i've created use different browsers and click ok while i'm here i'll take the opportunity to change the text i'll just use the same text i used earlier wikime and then i can click away from that button to have the system created so at this point you can choose any browser you like from the drop-down list and click your wiki me button to get a new random article from wikipedia and so that should all work fairly straightforwardly as i said at the start of the video it's not meant to be a particularly complicated example just the basics of how you can switch between different browsers um when you're using selenium so i hope you found that one useful thanks for watching it see you next time
Info
Channel: WiseOwlTutorials
Views: 3,664
Rating: 5 out of 5
Keywords: vba, excel, visual basic for applications, selenium, web scraping, scraping, websites, chrome, edge, opera, firefox, browser, webdriver, interface, chromedriver, edgedriver, operadriver, firefoxdriver, wise owl
Id: qxNx12RWihU
Channel Id: undefined
Length: 17min 46sec (1066 seconds)
Published: Sun Jan 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.