Now, one of the best tools for browser automation is selenium.
And Selenium works not just with Python, but with lots of programming languages.
But what it gives us is the ability to browse or use a browser without a human involved.
Just through code, we can act and move around the mouse, click and buttons and interact with a website.
And in this section, we're going to be using selenium.
Now in order to install selenium.
As you can see, we'll have to use pip install.
Now at the bottom here, you see that it says you may consider using virtual amp to create isolated
python environments.
Now, we already know about this, right?
You could install pip install like this and have it available across your entire machine or you can
use something like pip EMF to install it just for your specific project.
Or you can use Pye charm and use their automatic virtual environments.
Now we covered those topics in the other videos, so I won't here.
In my case, I'm just going to install it globally like this.
So I'm going to get started.
First we're going to install selenium.
So I'm going to say Pip, in my case, PIP three install selenium.
Awesome.
Now, once you have selenium installed, you can create a file.
In my case, I have an automation pi file here.
Let's make this a little bit bigger and this a little bit bigger.
Let me clear this.
Now, the next step is to, well, import in our file so I can say import or actually from selenium
package that we just installed.
We can say import Web driver.
Now, Web driver is going to allow us to drive the web through code.
However, in order for us to be able to use selenium properly, if you go to the installation page here.
You see that there's something called drivers.
You see, Selenium works with these drivers because each browser, like Chrome, Edge, Firefox and
Safari, has different drivers to interface with the different browsers.
So based on what browser you want to use to automate things, you have to install each of these drivers.
In our case, let's use the chrome driver.
So we would just click over here and you can see over here that we have the different chrome drivers
that we can use.
You have the current releases.
So the latest versions.
Now, usually the latest releases are not what we call stable.
That is, maybe they're just brand new, so they haven't been tested enough.
So what I like to do is to actually go to the Chrome Driver tab here and make sure that we select the
latest stable release.
So based on when you watch this video, this version number might be a little bit higher.
But as long as it's a stable release, instead of the latest beta release, which is chrome driver 76,
then that's fine.
So let's click over here and you'll see that as soon as I click here, it will give me zip files that
I can install based on my machine.
Now, in my case, I'm using a mac, so I'll just install the mac driver.
And once I open up the zip file.
You'll see that I get this chrome driver executable.
Now, an executable is kind of like a dot exec on a Windows machine.
If I double click this, it's going to run some code.
It's some binary code that we can run on the machine.
But this is our chrome driver.
This is going to allow selenium to be able to open up our Chrome browser and manipulate that browser.
Now in my case, I'm going to move this to my desktop because that's where my automation Python file
is.
There we go.
Now, if I close this and by the way, if you go to Pi Pi and click on selenium.
The package.
It'll actually explain some of these installation steps as well.
So you can actually.
Run through this quick set up as well.
Now, in our case, now that we have the chrome driver, what we need to do is to now create an instance
of the browser.
So let's just call this chrome.
Browser.
And this is going to equal the web driver that we just imported.
And then in here use Chrome as the browser and then inside as the parameter.
Remember, this is the Chrome class that web driver gives us.
We have to give it the driver in order to drive the browser.
Now because our chrome driver is in the same directory as my file.
I can just do a dot slash or use a string and say chrome.
Driver, which is the name of the driver.
Now let's make this a little bit smaller so we can see everything.
Now in here.
If we had multiple projects, we'd ideally use Chrome Driver, not just copy and paste Chrome Driver
everywhere into our project folders, but instead you'd put it into your path.
What does that mean?
Well, just like when we installed selenium, when we did PIP three install selenium.
Usually when you install something globally or when you have it available globally, if you're on a
mac or Linux computer, you usually will put it under.
User.
Local bin folder.
And you'd put it inside of this folder.
If you're on a windows, you'd usually put it in its path in the Windows folder of the C drive.
And if you do that, you can actually just keep this as blank and the driver will actually look for
the chrome driver in this path.
But in our case, we'll just leave it like this.
So make sure that the chrome driver is in the same folder as the file.
Now in here, let's print out what we have as a Chrome browser.
So I'm going to just run this file.
Oh, and I get an error during handling of the above exception.
All right.
It looks like I got a file not found here, and that's because I made a little typo here.
It should be chrome driver.
So let's say that again.
Give that another go.
And did you see that?
Chrome just opened up for me.
So right now automatically by running Chrome browser, I got an instance of the Chrome browser.
So let's see what we can do here.
Let's say we change our browser to just be like this.
If I go here and do chrome browser dot.
Maximize window and run this method.
Let's run our file.
Now, when we run this, nothing happened.
And the reason is you see over here that we get an air and you might get this as well.
It says Not created exception message session not created.
This version of Chrome Driver only supports Chrome version 75.
So you have to make sure that the driver that you install in our case it was the stable one is compatible
with the version of Chrome that you have.
Most likely you'll have to update your Chrome version.
So you see over here that I have a version 74.
But in here it says, hey, this is compatible only with Chrome version 75.
So how can we fix this?
We're simply going to relaunch.
I've updated my Chrome version again.
If you're not sure how to do that, you simply go to Chrome and go to about Google Chrome or preferences
and you'll be able to update to the latest version or make sure that you're using the version that matches
your driver.
Now that we have version 75, let's try this again.
I'm going to run my file.
And look at that.
I get chrome is being controlled by an automated test software.
So I've just created a new.
Window, a new instance, which is what we've done here with the Chrome browser and I've just said maximise
window.
Now if I remove maximise window here.
And I simply run this again.
You see that right now?
I have another window.
This time with the window.
Well, now maximized, just the default sizing.
So every time I run this, I create a new instance of the browser.
Very, very cool.
Now.
Let's close these for now.
And in the next video, we're going to explore how we can use this to actually move through Web pages.
Now, if you haven't gotten this far, take your time to make sure that the versions match, that you're
able to run a browser from your command line.
And in the next video, we're going to do some really cool things.
I'll see you in that one.
Bye bye.