1
00:00:00,360 --> 00:00:03,360
In order to start using Selenium WebDriver,

2
00:00:03,660 --> 00:00:07,290
we're going to have to go through a number of steps in order to set up our

3
00:00:07,290 --> 00:00:11,970
browser and also to install and set up Selenium. Now,

4
00:00:12,120 --> 00:00:16,830
the first step, if you haven't already, is to install the Chrome browser.

5
00:00:17,100 --> 00:00:19,860
Now, while you can work with selenium using other browsers

6
00:00:19,880 --> 00:00:22,460
like Firefox or Safari,

7
00:00:22,850 --> 00:00:25,850
we're going to be using a number of Chrome developer tools. And also

8
00:00:26,390 --> 00:00:29,900
in order for you to work alongside, to do exactly what I'm doing,

9
00:00:30,200 --> 00:00:33,260
it'll be much easier if you're working on the same browser and the same

10
00:00:33,260 --> 00:00:36,020
technology. If you haven't already got Chrome,

11
00:00:36,050 --> 00:00:40,100
then head over to the course resources where you'll find a link to download the

12
00:00:40,100 --> 00:00:44,390
latest version for your computer. And then once you've done that,

13
00:00:44,510 --> 00:00:47,810
the next step is to download the Chrome driver.

14
00:00:48,260 --> 00:00:53,180
And this is different from the actual browser. Again, in the course resources,

15
00:00:53,210 --> 00:00:58,210
you'll find a link to all the Chromedrivers and we have to download the

16
00:00:58,280 --> 00:01:00,350
version of Chromedriver that matches

17
00:01:00,980 --> 00:01:05,690
the version of Chrome that we have. In order to find out the version of Chrome,

18
00:01:05,720 --> 00:01:08,840
all you have to do head over to the top-right corner,

19
00:01:08,870 --> 00:01:11,420
click on these three dots, go to help,

20
00:01:11,690 --> 00:01:14,930
and then go to about Google Chrome. Here

21
00:01:14,930 --> 00:01:18,170
it's going to tell you which version you are currently running.

22
00:01:18,590 --> 00:01:21,650
And because you're going to be watching the video in the future,

23
00:01:21,920 --> 00:01:26,920
this version might not be the same for you as you see right here. Currently,

24
00:01:27,080 --> 00:01:29,630
I am completely up to date, so the latest 

25
00:01:29,630 --> 00:01:33,950
version that I'm running is version 84. Now,

26
00:01:33,980 --> 00:01:38,980
all I have to do is go back here and find the version of 84 and download it.

27
00:01:39,800 --> 00:01:43,850
So I just have to click here and it will take me to a

28
00:01:43,910 --> 00:01:47,030
page where I've got three options. Now

29
00:01:47,030 --> 00:01:51,470
if I'm running a Linux computer, then I click on this one. If I'm running a Mac,

30
00:01:51,500 --> 00:01:55,160
I click on this one and Windows users will click on this one.

31
00:01:55,310 --> 00:01:59,150
So I'm running a Mac so I'm going to take that particular file.

32
00:01:59,870 --> 00:02:02,420
Now I'm going to have to unzip the file

33
00:02:02,720 --> 00:02:05,420
and I end up with the actual Chrome driver.

34
00:02:06,350 --> 00:02:11,350
Now the final step is to move this Chrome driver to a location of our choice.

35
00:02:12,800 --> 00:02:16,580
I don't recommend keeping the driver inside the downloads folder

36
00:02:16,610 --> 00:02:21,080
just because I'm sure you, as well as I, will and delete everything in the

37
00:02:21,080 --> 00:02:23,630
downloads folder without a lot of inspection.

38
00:02:24,080 --> 00:02:26,870
So we should move this Chrome driver to somewhere else,

39
00:02:26,990 --> 00:02:31,120
somewhere where it's not likely to get deleted accidentally. In my case,

40
00:02:31,250 --> 00:02:36,250
I'm going to go up a level with command up and here 

41
00:02:36,380 --> 00:02:39,350
I've got a folder that I've created called Development.

42
00:02:39,860 --> 00:02:43,730
Now you can create a folder anywhere you like, um,

43
00:02:43,820 --> 00:02:48,820
and then call it anything you like and move this Chrome driver over to the new

44
00:02:49,940 --> 00:02:53,540
location. Now, the most important thing though is

45
00:02:53,810 --> 00:02:58,810
to actually get the file path for this particular file. To do that on a Mac

46
00:02:59,800 --> 00:03:03,730
all we have to do is right-click and go to get info.

47
00:03:04,210 --> 00:03:09,210
And then where it says where we're going to copy all of that hit command+C to

48
00:03:09,850 --> 00:03:14,530
copy. And then we're going to go into a new project that we've created

49
00:03:14,650 --> 00:03:18,130
which I've named day-48. In the main.py

50
00:03:18,190 --> 00:03:23,190
I'm going to paste this entire file path and I'm gonna save it as the Chrome

51
00:03:24,070 --> 00:03:26,200
driver path like this.

52
00:03:27,790 --> 00:03:32,790
Now the final step we have to add to this path is to add a forward slash to the

53
00:03:33,850 --> 00:03:35,590
actual Chrome driver.

54
00:03:36,430 --> 00:03:39,580
PyCharm should auto suggest the name of Chrome driver,

55
00:03:39,940 --> 00:03:44,320
but this is now the full path that will take us to that particular driver.

56
00:03:45,970 --> 00:03:49,840
Now, if you are on Windows, the process is pretty much the same.

57
00:03:50,050 --> 00:03:54,820
Once you've downloaded the driver for your system, so Windows 32,

58
00:03:55,420 --> 00:03:59,980
then you're going to go ahead and open it up and unzip it.

59
00:03:59,980 --> 00:04:04,980
So click on this extract button so that you can actually extract everything

60
00:04:05,830 --> 00:04:07,900
that's inside. Now,

61
00:04:07,900 --> 00:04:12,400
it's going to ask you for a destination for where you want to put the extracted

62
00:04:12,400 --> 00:04:12,820
file.

63
00:04:12,820 --> 00:04:17,230
So we're going to click on browse and you're gonna select a location on your

64
00:04:17,230 --> 00:04:20,440
computer. Now, if you don't have a preference,

65
00:04:20,440 --> 00:04:25,440
I recommend just going to your C drive and then here creating a new folder

66
00:04:30,880 --> 00:04:32,230
called Development.

67
00:04:32,740 --> 00:04:37,210
And then inside this folder is where you're going to extract the Chrome driver.

68
00:04:38,020 --> 00:04:42,100
So go ahead and click extract. And then once that's done,

69
00:04:42,130 --> 00:04:44,800
we'll go and navigate to that location.

70
00:04:45,100 --> 00:04:49,090
So it was in the C drive and it was inside the development folder.

71
00:04:49,420 --> 00:04:53,440
And you can check and just make sure that your Chrome driver is sitting inside

72
00:04:53,440 --> 00:04:53,830
here

73
00:04:53,830 --> 00:04:58,830
and it's an application that you've extracted from the zip file. Now to find out

74
00:04:59,050 --> 00:05:03,250
its file path, we're going to right-click and go to properties.

75
00:05:03,760 --> 00:05:07,330
And then in the properties you see the location right here.

76
00:05:07,960 --> 00:05:10,180
So here is the location

77
00:05:10,240 --> 00:05:15,160
and all we have to do is just to select everything that's here and then go ahead

78
00:05:15,160 --> 00:05:17,500
and use control+c to copy it

79
00:05:18,070 --> 00:05:22,060
and then we can paste it into our file in PyCharm.

80
00:05:22,720 --> 00:05:27,190
So we're going to paste it inside a string like this. And again,

81
00:05:27,220 --> 00:05:31,540
we're going to save it into the name that is chrome_

82
00:05:31,990 --> 00:05:36,010
driver_path like this.

83
00:05:36,610 --> 00:05:39,370
And again, we're doing the same thing that we did with the Mac.

84
00:05:39,400 --> 00:05:44,400
We're going to add another backslash and then add the name of our Chrome driver

85
00:05:45,760 --> 00:05:46,593
like this.

86
00:05:47,230 --> 00:05:51,220
So now that we've installed Chrome and we've downloaded the Chrome driver,

87
00:05:51,760 --> 00:05:56,760
the next and last step is to install and set up Selenium. In my main.py

88
00:05:57,560 --> 00:06:02,560
I'm going to import Selenium. And make sure that you spelled it correctly

89
00:06:02,990 --> 00:06:04,460
so it looks like this.

90
00:06:04,820 --> 00:06:09,620
And then we're going to click on this red light bulb as always to install our

91
00:06:09,620 --> 00:06:12,020
package. Now,

92
00:06:12,020 --> 00:06:14,750
once Selenium has been installed successfully,

93
00:06:15,200 --> 00:06:17,270
I'm going to change the import statement.

94
00:06:17,600 --> 00:06:21,980
I'm going to say that from this newly installed selenium package,

95
00:06:22,280 --> 00:06:27,280
I'm going to import a module called WebDriver and it's this webdriver

96
00:06:28,310 --> 00:06:33,050
which is going to be driving the Chrome browser and doing all of our automated

97
00:06:33,050 --> 00:06:33,883
tasks.

98
00:06:34,250 --> 00:06:38,870
Let's create a new driver from that new module.

99
00:06:39,410 --> 00:06:44,410
So driver = webdriver. and then we can now instantiate any browser of our

100
00:06:45,980 --> 00:06:50,510
choice. So in our case, we're going to be creating a Chrome browser to drive,

101
00:06:50,930 --> 00:06:55,930
but you can see that you can also have Firefox or Safari and a large number of

102
00:06:56,270 --> 00:06:57,140
other browsers.

103
00:06:58,280 --> 00:07:02,930
We're going to choose Chrome and notice that this is Chrome with a capital C.

104
00:07:02,960 --> 00:07:05,150
So we're initializing a new object here.

105
00:07:05,900 --> 00:07:10,900
And the only required argument we have to give is the executable path.

106
00:07:11,450 --> 00:07:14,720
So this is the path of the executable

107
00:07:14,870 --> 00:07:17,630
that is the Chrome driver that we downloaded previously.

108
00:07:18,200 --> 00:07:22,910
This is going to be the path and notice that it's the location where that Chrome

109
00:07:22,910 --> 00:07:24,800
driver lives. And then at the very end,

110
00:07:24,830 --> 00:07:27,860
we've got the actual name of the executable

111
00:07:27,890 --> 00:07:32,750
which on a Mac is called chromedriver, on Windows it's chromedriver.exe.

112
00:07:33,590 --> 00:07:38,150
Let's go ahead and pass this in here. And now that we've created our driver,

113
00:07:38,180 --> 00:07:42,050
we're pretty much done with installing and setting up selenium WebDriver.

114
00:07:42,800 --> 00:07:45,770
But if you're wondering what this Chrome driver is, well,

115
00:07:45,800 --> 00:07:49,340
here's the way to think about it. We've got our Selenium package

116
00:07:49,580 --> 00:07:53,180
which contains code for us to be able to interact with browsers.

117
00:07:53,750 --> 00:07:57,890
Now it can interact with the Chrome browser which is what we're choosing to do,

118
00:07:58,250 --> 00:08:02,450
but it can also interact with a bunch of other ones like Safari or Firefox.

119
00:08:03,140 --> 00:08:03,500
Now,

120
00:08:03,500 --> 00:08:08,500
how do we make sure that this package which can handle all three knows

121
00:08:08,540 --> 00:08:12,800
specifically how to work with the Chrome browser? Well,

122
00:08:12,800 --> 00:08:17,800
we're going to need some sort of a bridge that bridges the selenium code to work

123
00:08:19,550 --> 00:08:21,380
with the Chrome browser.

124
00:08:21,860 --> 00:08:25,220
And this bridge is provided by the Chrome driver.

125
00:08:25,940 --> 00:08:28,280
So there'll be a different driver for a Safari,

126
00:08:28,310 --> 00:08:30,500
and there'll be a different driver for Firefox.

127
00:08:30,920 --> 00:08:33,320
And just by switching up those drivers,

128
00:08:33,620 --> 00:08:37,880
it will tell selenium how to work with the latest version of these browsers.

129
00:08:38,540 --> 00:08:40,580
All we have to do is now use it.

130
00:08:41,210 --> 00:08:45,560
So the first thing I'm going to use my driver to do is I'm just going to get it

131
00:08:45,620 --> 00:08:49,490
to open up a webpage. We'll use driver.get

132
00:08:49,640 --> 00:08:52,070
and then we can pass in the URL here.

133
00:08:52,730 --> 00:08:55,100
So it's going to be HTTPS

134
00:08:55,140 --> 00:08:59,430
and then I'm going to go to amazon.com. Now,

135
00:08:59,730 --> 00:09:02,370
if you're on Windows and you go ahead and run the code,

136
00:09:02,430 --> 00:09:06,240
then it should run without any issues. But if you're on a Mac, 

137
00:09:06,270 --> 00:09:07,530
this is what happens.

138
00:09:09,720 --> 00:09:14,040
You'll get a popup that says chromedriver cannot be opened because the developer

139
00:09:14,040 --> 00:09:18,870
cannot be verified even though we know that this is actually created by Google,

140
00:09:18,900 --> 00:09:22,860
specifically for the Chrome browser. So if this happens,

141
00:09:22,890 --> 00:09:24,210
you're going to click cancel

142
00:09:24,540 --> 00:09:29,430
and you'll get all of these errors because it's unable to access that file. Now,

143
00:09:29,460 --> 00:09:34,170
in order to enable it, we're going to go to the Apple symbol, system preferences.

144
00:09:35,940 --> 00:09:39,390
And then we're going to go to security and privacy and here,

145
00:09:39,390 --> 00:09:41,370
because we try to run it before,

146
00:09:41,610 --> 00:09:45,060
we've got something that says chromedriver was blocked from use.

147
00:09:45,450 --> 00:09:48,120
So we're going to click on the allow anyways.

148
00:09:48,690 --> 00:09:52,890
And now if we run this again, then it'll give us this popup

149
00:09:53,070 --> 00:09:55,350
and all we have to do is click open.

150
00:09:56,190 --> 00:09:58,380
And once we've done all of that,

151
00:09:58,680 --> 00:10:03,680
now it's going to finally open up the site that we wanted to go to, amazon.com,

152
00:10:05,010 --> 00:10:08,370
using this automated version of Chrome.

153
00:10:08,760 --> 00:10:11,910
And you can see this because right at the top here,

154
00:10:11,970 --> 00:10:15,330
it says Chrome is being controlled by automated test software.

155
00:10:15,630 --> 00:10:17,040
That is our selenium.

156
00:10:17,700 --> 00:10:22,700
The driver.get method effectively opens up a new browser window with the URL

157
00:10:24,300 --> 00:10:25,410
that you've specified.

158
00:10:26,040 --> 00:10:31,040
Now you'll also want to close down the window or the entire browser once you're

159
00:10:32,640 --> 00:10:37,640
done with whatever it is you may want to automate or do. To do that

160
00:10:38,010 --> 00:10:42,960
all we have to do is tap into driver and then we can call the method close.

161
00:10:43,350 --> 00:10:45,420
Now, when I hit the run button,

162
00:10:45,480 --> 00:10:48,600
you can see now it's going to open up amazon.com

163
00:10:48,930 --> 00:10:52,800
and as soon as that page loaded, it pretty much just closed it down.

164
00:10:53,550 --> 00:10:58,290
Now there's also another method called driver.quit.

165
00:10:58,740 --> 00:11:00,840
Now what's the difference between close and quit,

166
00:11:00,870 --> 00:11:05,700
you might wonder? Well, close actually just closes a single tab,

167
00:11:05,760 --> 00:11:09,510
the active tab where you've opened up a particular page.

168
00:11:09,930 --> 00:11:13,020
Now quit is actually going to quit the entire browser.

169
00:11:13,530 --> 00:11:17,280
There are certain cases where you have more than one tab running at the same

170
00:11:17,280 --> 00:11:18,113
time.

171
00:11:18,120 --> 00:11:22,830
Close will close that particular tab while quit will actually quit the entire

172
00:11:22,830 --> 00:11:26,160
program. Once I'm done using my driver,

173
00:11:26,190 --> 00:11:31,190
I prefer actually just using quit to shut down the entire browser.

174
00:11:32,040 --> 00:11:36,240
That way we can always start again from scratch and we'll have a fresh browser

175
00:11:36,240 --> 00:11:39,870
to work with. And in the next and coming lessons,

176
00:11:39,990 --> 00:11:44,040
we're going to show you how to use selenium to do basically anything that we

177
00:11:44,040 --> 00:11:46,500
could do with a mouse and keyboard as humans,

178
00:11:46,770 --> 00:11:50,880
but instead, we're going to automate the entire process and workflow.

179
00:11:51,450 --> 00:11:54,540
So for all of that and more, I'll see you in the next lesson.

