Excel VBA Introduction Part 57.1 - Getting Started with Selenium Basic and Google Chrome

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to this weizel vba tutorial in this video we'll explain how to get started with selenium basic and google chrome so the video is all about how to get selenium basic and the chrome driver installed so that you can start writing vba code to manipulate google chrome we'll start with a brief explanation of what selenium is although feel free to skip that section if you're not interested and then we'll explain how to download and install the selenium libraries for visual basic applications we'll also talk about how you download drivers for various different web browsers and we'll run an example for installing the driver for google chrome then we'll explain how to reference selenium in a vba project and write some very basic code to open up a new instance of google chrome and navigate to a website so let's get started just before i show you how to install selenium for vba i thought it was worthwhile quickly talking about the basic background of what selenium is of course if you're not interested in this feel free to skip ahead to the next chapter where we'll get straight into the installation part but if you are interested a good place to start with learning about selenium is the official documentation website and by the way there are quite a lot of links you'll need in this video and i'll post all these links in the description below so you'll have them all to hand so selenium is basically a set of libraries which allow you to manipulate web browsers and the great thing about it is it's not specific to any one particular web browser in principle you can use the same basic code to control chrome or firefox or safari or even if you still wanted to internet explorer so unlike the other videos we have in our playlist which primarily use internet explorer selenium allows you to manipulate pretty much any browser you can think of so this website contains lots of useful information for working with selenium including an installation guide and a general guidelines page and even a worst practices page i'd recommend giving that page a read through by the way even if you just want to jump head first into playing around with selenium read the worst practices page it could potentially save you some long-term pain one thing you'll notice as you work through this website however is that it notably lacks any reference whatsoever to vba so there are lots of code samples littered around for a variety of languages but no mention of vba whatsoever and that's simply because selenium was developed without vpa in mind fortunately for us because selenium is an open source project an enterprising and incredibly helpful person has ported the selenium libraries to a vba version called selenium basic and that's what we're going to show you how to install in this video to download selenium basic you'll need to head over to this page on the github website and again i'll post this link in the video description and then you'll need to find in the assets section the executable for the most recent release of selenium basic you can simply click on that link to start downloading the file and when that's happened we'll have a look at what we end up with okay now the file's been downloaded i can simply launch the executable by clicking on the file download of course i'm using google chrome for this you may be using a different browser so however however you launch files that you've downloaded just do that whichever browser you're using and that launches the setup wizard for selenium so we can click next and then have a good read of the license agreement blah blah blah blah good and then choose to accept the agreement and then click next and at this point you get to choose exactly which components of selenium basic you want to install you'll notice a lot of the items in this list are called web drivers so in order for selenium to communicate with a particular web browser it needs a driver for that browser it's highly likely that you're not going to be using every single browser listed here so you might want to uncheck the web drivers for the ones you're not going to use and it's also quite likely that the web driver that's included in the default installation isn't the most up-to-date one even if it is for the browser you want to use so my recommendation would be actually to set the installation to a compact installation so that removes all the web drivers and just includes basically the core components including a few a few examples as well so at that point i'm going to click next i get a quick summary of what's going to happen then i can click install and it will now install the software doesn't take too long it's not particularly large installation and when it's done you can simply click finish so we have the selenium library installed for vba but it doesn't currently have any way to communicate with a web browser because i elected not to install any web drivers to find out which drivers you need to install a good place to start again is back on the selenium documentation site in here you'll find a link on the left hand side for getting started with webdriver and then in there will be a section for browsers and then you'll get a list of links for the main web browsers you're likely to use for this example i'm going to install the driver for google chrome so i'm going to open up the um the link to the chromium or chrome driver page i'll need to know which version of the chrome driver to install um if i want to see all of the available versions i can hit the downloads link here to find out which version of chrome i'm using a good place to do that is from the the extra options menu at the top right hand corner of google chrome from there i can choose help and then i can choose about google chrome and on the page that appears it will show me exactly which version i'm currently using so the the main number there is 87 so i can refer back to on the downloads page the main version for chrome 87 and then i can simply click the link next to that one to start downloading it in fact i need to find the correct driver first so i'm in a windows environment so i'm going to download the driver chrome driver for version 87 and the win32 version so i can click on that to start downloading that file now that i've downloaded the driver i'll need to extract the file and copy it to the relevant location to do that i'm going to click on the little up arrow on my file download and choose show in folder i can then right click on my chrome driver zip file and choose to extract it and i'll choose to show the extracted files when completed so i'll click extract and then i'll get a single file called chromedriver.exe i don't need to launch that all i need to do is copy it to the correct location so i'm just going to copy that file with control c the location you need to copy it to is the installation folder for selenium now in the little install wizard you didn't get a choice as to where that was installed and there are a couple of different possibilities so one possibility is it was installed to the program files folder in the root of your c drive and but that's not the case for me here another option is it could have been installed to your local folder in the app data of the current user and that's the case for my particular example so in my user's username app data local folder i'll find a folder for selenium basic and there it is so i can double click to open up that folder and i simply need to paste in the chrome driver file i'm going to do that by pressing ctrl and v and now it's in the correct location at this point i think it's worthwhile quickly checking that everything is actually going to work so we're not going to do too much in the way of scraping a website but just to establish that we can actually control google chrome using vba i have opened up a blank excel workbook i'm going to head to the developer tab and then open up the visual basic editor and then in here i'm going to set a reference to the selenium library to do that i can head to the tools menu and choose references and then in my list i can scroll down to find selenium if i can remember my alphabet so i will find the selenium type library listed there i can simply check the box next to that and then click ok now that i've referenced the selenium library i can get a bit of basic help on it using the object browser so i can open the object browser either by clicking on the object browser button here on the toolbar or heading to the view menu and choosing object browser or just by pressing f2 from the drop-down list at the top where it says all libraries i can select the selenium library to filter the list and then i'll see a list of all of the classes defined in this library i can select a class so for example i'll select the chrome driver class and then in the list on the right hand side i'll see all the methods and properties of that class and although the help is quite basic it's useful to click on a method or a property to get a quick idea as to what it does so for example if i click on the get method down here i'll see a brief description of to what the get method does so it loads a new web page and tells me the parameter list whether it's required or optional so it's basic but still kind of helpful to get a brief feel for how the classes are set up but at this point i'm just going to close down the object browser and then we'll insert a new module into our project before we write some code to actually manipulate chrome so i'm going to right click on the project choose insert and then choose module so let's write a basic subroutine to test selenium for want of a better name i'll call mine test selenium if you can think of a better name please feel free to use it now there are many ways to do what we're about to do and we'll go into more detail in later videos in the series but just for the absolute basics i'd like to declare a variable which can hold a reference to a chrome web browser so i'm going to call this i'll call it my browser as selenium.chrome driver very much not the only way to do this but as i say we'll explain more detail about exactly how to make this work in later videos i then want to set a reference to a new instance of the chrome driver so i'm going to say set my browser equals new selenium.chrome browser i'm sorry backpardon chrome driver and then i simply want to start it so i'm going to say my browser dot start okay having done that i'm just going to quickly run this subroutine and you'll see that i get a brief flash of the chrome browser popping up on my screen and then it immediately disappears when the my browser variable goes out of scope so essentially because this brow because this variable is defined in this subroutine when the subroutine ends the variable becomes out of scope it destroys any instance of the browser that we've created a fairly simple way to solve that problem is to declare this variable at the level of the module so if i just move my variable declaration up to just below the option explicit statement but before my subroutine begins i can leave it declared with the dim keyword it's more conventional to use the private or public keywords when you declare variables at the top of a module but now that i've done this if i run my subroutine again it will open up google chrome and we'll leave it sitting there on screen even when the code is finished so i'm actually going to manually close this down because it's not much use when you don't actually navigate to a web page to navigate to a web page again there are several different ways to do this but there's a very basic example i'm going to use the get method on the browser that i've just started so i can say my browser if i can type in my browser there we go dot get and then a space followed by the url that i want to navigate to so i can pass this in as a string what should i go for well let's go for wiseal dot code uk so https colon four slash forward slash www.wisel.co.uk which is a site i'm sure you all know off by heart by now and then if i were to execute this subroutine again i'll find that chrome opens up and then it navigates to the homepage for the ysl website perfect so i'll just manually close that one down okay i appreciate that's a very basic example and we haven't actually done anything useful with the web page but the point of this video was to help you get selenium basic installed so that you're set up and ready to go to do all the exciting things we're going to do in the next few videos in this playlist so hope you found that one useful thanks for watching see you next time
Info
Channel: WiseOwlTutorials
Views: 17,775
Rating: 4.9624414 out of 5
Keywords: vba, excel, visual basic for applications, web scraping, web browser, selenium, seleniumbasic, selenium basic, google chrome, firefox, opera, safari, internet explorer, edge, mozilla, scrape, scraping, webscraping, wise owl
Id: FoxWcvZzYVk
Channel Id: undefined
Length: 12min 32sec (752 seconds)
Published: Thu Jan 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.