Instructor: In order to start using Selenium Webdriver,
we're going to have to go through
a number of steps in order to set up our browser
and also to install and set up Selenium.
Now, the first step, if you haven't already
is to install the Chrome browser.
Now, while you can work with Selenium using other browsers
like Firefox or Safari, we're going to be using a number
of Chrome developer tools and also in order
for you to work alongside to do exactly what I'm doing
it'll be much easier if you're working
on the same browser and the same technology.
If you haven't already got Chrome than head over
to the course resources where you'll find a link
to download the latest version for your computer.
So now that we've installed Chrome
the next and last step is to install and set up Selenium.
In my main.py, I'm going to import Selenium
and make sure that you spelt it correctly
so it looks like this.
And then we're gonna click
on this red light bulb as always to install our package.
Now, once Selenium has been installed successfully
I'm going to change the import statement.
I'm going to say that
from this newly installed Selenium package
I'm going to import a module called webdriver
and it's this web driver
which is going to be driving the Chrome browser
and doing all of our automated tasks.
Let's create a new driver from that new module.
So driver = webdriver.
and then we can now instantiate any browser of our choice.
So in our case we're gonna be creating a Chrome browser
to drive but you can see that you can also have Firefox
or Safari and a large number of other browsers.
We're going to choose Chrome
and notice that this is Chrome with a capital C.
So we're initializing a new object here.
And now that we've created our driver
we're pretty much done
with installing and setting up Selenium Webdriver.
But if you're wondering what this Chrome driver is,
well here's the way to think about it.
We've got our Selenium package, which contains code
for us to be able to interact with browsers.
Now, it can interact with the Chrome browser
which is what we're choosing to do
but it can also interact
with a bunch of other ones like Safari or Firefox.
Now, how do we make sure that this package
which can handle all three
knows specifically how to work with the Chrome browser?
Well, we're gonna need some sort
of a bridge that bridges the Selenium code to work
with the Chrome browser.
And this bridge is provided by the Chrome driver.
So there'll be a different driver
for Safari and there'll be a different driver for Firefox.
And just by switching up those drivers
it'll tell Selenium how to work
with the latest version of these browsers.
All we have to do is now use it.
So the first thing I'm gonna use my driver
to do is I'm just going to get it to open up a webpage.
We'll use driver.get
and then we can pass in the URL here.
It's going to be https
and then I'm going to go to amazon.com.
Now if you're on Windows and you go ahead and run the code
then it should run without any issues.
But if you're on a Mac, this is what happens.
You'll get a popup that says Chrome driver cannot be opened
because the developer cannot be verified
even though we know that this is actually created
by Google specifically for the Chrome browser.
So if this happens,
you're gonna click cancel and you'll get all
of these errors because it's unable to access that file.
Now, in order to enable it
we're going to go to the Apple symbol system preferences
and then we're gonna go to security and privacy.
And here, because we tried to run it before,
we've got something that says
Chrome driver was blocked from use.
So we're gonna click on the allow anyways.
And now if we run this again, then it'll give us this popup
and all we have to do is click open.
And once we've done all of that,
now it's going to finally open up the site
that we wanted to go to amazon.com
using this automated version of Chrome.
And you can see this because at the right at the top here
it says Chrome is being controlled
by automated test software.
That is our Selenium.
However, you'll also notice
that the Chrome browser is also closed immediately
after our program finishes running.
So in order to keep the browser open
after our program finishes
we have to configure our web driver.
So to do that, we have to get hold of the Chrome options.
We can get those via webdriver.chrome options
and then we have to add an experimental option in Chrome
called detach and set that option to true.
Now we pass that configuration over
to our web driver via parameter called options.
And now when we run our Python program,
our Chrome browser
doesn't close itself automatically anymore.
So we've looked at configuring our browser to keep it open
but we can also close the tabs
and quit Chrome programmatically.
To do that, all we have to do is tap into driver
and then we can call the method close.
Now, when I hit the run button
you can see now it's gonna open up amazon.com.
as soon as that page loaded
it pretty much just closed it down.
Now there's also another method called driver.quit.
Now what's the difference between close and quit,
you might wonder.
Well, close actually just closes a single tab,
the active tab where you've opened up a particular page.
Now quit is actually gonna quit the entire browser.
So there are certain cases where you have
more than one tab running at the same time.
Close will close that particular tab
while quit will actually quit the entire program.
Once I'm done using my driver,
I prefer actually just using quit
to shut down the entire browser.
That way we can always start again
from scratch and we'll have a fresh browser to work with.
And in the next and coming lessons
we're gonna show you how to use Selenium
to do basically anything that we could do
with a mouse and keyboard as humans
but instead we're going to automate
the entire process and workflow.
So for all of that and more,
I'll see you on the next lesson.