WEBVTT

0
00:00.330 --> 00:03.690
In order to start using Selenium Webdriver,

1
00:03.690 --> 00:04.830
we're going to have to go through

2
00:04.830 --> 00:08.340
a number of steps in order to set up our browser

3
00:08.340 --> 00:11.610
and also to install and set up Selenium.

4
00:11.610 --> 00:14.190
Now, the first step, if you haven't already,

5
00:14.190 --> 00:17.100
is to install the Chrome browser.

6
00:17.100 --> 00:20.370
Now, while you can work with Selenium using other browsers

7
00:20.370 --> 00:24.270
like Firefox or Safari, we're going to be using a number

8
00:24.270 --> 00:26.760
of Chrome Developer Tools, and also in order

9
00:26.760 --> 00:30.210
for you to work alongside to do exactly what I'm doing

10
00:30.210 --> 00:31.860
it'll be much easier if you're working

11
00:31.860 --> 00:34.650
on the same browser and the same technology.

12
00:34.650 --> 00:36.660
If you haven't already got Chrome than head over

13
00:36.660 --> 00:39.330
to the Course Resources where you'll find a link

14
00:39.330 --> 00:43.260
to download the latest version for your computer.

15
00:43.260 --> 00:45.780
So now that we've installed Chrome,

16
00:45.780 --> 00:50.190
the next and last step is to install and set up Selenium.

17
00:50.190 --> 00:53.680
In my main.py, I'm going to import selenium

18
00:54.780 --> 00:57.030
and make sure that you spelt it correctly

19
00:57.030 --> 00:58.830
so it looks like this.

20
00:58.830 --> 00:59.820
And then we're going to click

21
00:59.820 --> 01:04.473
on this red light bulb as always to install our package.

22
01:05.820 --> 01:09.210
Now, once Selenium has been installed successfully

23
01:09.210 --> 01:11.610
I'm going to change the import statement.

24
01:11.610 --> 01:12.660
I'm going to say that

25
01:12.660 --> 01:16.290
from this newly installed selenium package

26
01:16.290 --> 01:20.490
I'm going to import a module called webdriver

27
01:20.490 --> 01:22.320
and it's this web driver

28
01:22.320 --> 01:25.170
which is going to be driving the Chrome browser

29
01:25.170 --> 01:28.290
and doing all of our automated tasks.

30
01:28.290 --> 01:33.290
Let's create a new driver from that new module.

31
01:33.450 --> 01:35.043
So driver = webdriver.

32
01:36.930 --> 01:40.650
and then we can now instantiate any browser of our choice.

33
01:40.650 --> 01:43.950
So in our case we're going to be creating a Chrome browser

34
01:43.950 --> 01:47.760
to drive, but you can see that you can also have Firefox

35
01:47.760 --> 01:51.333
or Safari and a large number of other browsers.

36
01:52.320 --> 01:54.210
We're going to choose Chrome

37
01:54.210 --> 01:57.000
and notice that this is Chrome with a capital C.

38
01:57.000 --> 01:59.343
So we're initializing a new object here.

39
02:00.210 --> 02:02.400
And now that we've created our driver

40
02:02.400 --> 02:03.660
we're pretty much done

41
02:03.660 --> 02:07.020
with installing and setting up Selenium Webdriver.

42
02:07.020 --> 02:09.660
But if you're wondering what this Chrome driver is,

43
02:09.660 --> 02:11.640
well here's the way to think about it.

44
02:11.640 --> 02:15.120
We've got our Selenium package, which contains code

45
02:15.120 --> 02:17.970
for us to be able to interact with browsers.

46
02:17.970 --> 02:20.460
Now, it can interact with the Chrome browser

47
02:20.460 --> 02:22.470
which is what we're choosing to do

48
02:22.470 --> 02:23.670
but it can also interact

49
02:23.670 --> 02:27.330
with a bunch of other ones like Safari or Firefox.

50
02:27.330 --> 02:30.150
Now, how do we make sure that this package

51
02:30.150 --> 02:32.280
which can handle all three

52
02:32.280 --> 02:35.523
knows specifically how to work with the Chrome browser?

53
02:36.750 --> 02:38.250
Well, we're going to need some sort

54
02:38.250 --> 02:43.250
of a bridge that bridges the Selenium code to work

55
02:43.770 --> 02:46.050
with the Chrome browser.

56
02:46.050 --> 02:50.160
And this bridge is provided by the Chrome driver.

57
02:50.160 --> 02:51.720
So there'll be a different driver

58
02:51.720 --> 02:55.140
for Safari and there'll be a different driver for Firefox.

59
02:55.140 --> 02:57.810
And just by switching up those drivers

60
02:57.810 --> 02:59.790
it'll tell Selenium how to work

61
02:59.790 --> 03:02.730
with the latest version of these browsers.

62
03:02.730 --> 03:05.430
All we have to do is now use it.

63
03:05.430 --> 03:07.230
So the first thing I'm going to use my driver

64
03:07.230 --> 03:12.180
to do is I'm just going to get it to open up a webpage.

65
03:12.180 --> 03:13.830
We'll use driver.get()

66
03:13.830 --> 03:16.980
and then we can pass in the URL here.

67
03:16.980 --> 03:19.020
It's going to be https

68
03:19.020 --> 03:22.590
and then I'm going to go to amazon.com.

69
03:22.590 --> 03:26.340
Now if you're on Windows and you go ahead and run the code

70
03:26.340 --> 03:28.800
then it should run without any issues,

71
03:28.800 --> 03:31.563
but if you're on a Mac, this is what happens.

72
03:33.600 --> 03:36.960
You'll get a popup that says, "chromedriver" cannot be opened

73
03:36.960 --> 03:39.750
because the developer cannot be verified",

74
03:39.750 --> 03:41.910
even though we know that this is actually created

75
03:41.910 --> 03:45.750
by Google specifically for the Chrome browser.

76
03:45.750 --> 03:46.800
So if this happens,

77
03:46.800 --> 03:49.260
you're going to click cancel and you'll get all

78
03:49.260 --> 03:53.100
of these errors because it's unable to access that file.

79
03:53.100 --> 03:54.570
Now, in order to enable it,

80
03:54.570 --> 03:58.120
we're going to go to the Apple symbol, System Preferences,

81
03:59.850 --> 04:02.610
and then we're going to go to Security & Privacy,

82
04:02.610 --> 04:05.520
and here, because we tried to run it before,

83
04:05.520 --> 04:07.050
we've got something that says,

84
04:07.050 --> 04:09.360
'"chromedriver" was blocked from use...'

85
04:09.360 --> 04:12.570
So we're going to click on the Allow Anyway.

86
04:12.570 --> 04:16.950
And now if we run this again, then it'll give us this popup

87
04:16.950 --> 04:20.070
and all we have to do is click open.

88
04:20.070 --> 04:22.560
And once we've done all of that,

89
04:22.560 --> 04:26.280
now it's going to finally open up the site

90
04:26.280 --> 04:28.920
that we wanted to go to amazon.com

91
04:28.920 --> 04:32.640
using this automated version of Chrome.

92
04:32.640 --> 04:35.820
And you can see this because at the right at the top here

93
04:35.820 --> 04:37.770
it says, "Chrome is being controlled

94
04:37.770 --> 04:39.510
by automated test software."

95
04:39.510 --> 04:41.730
That is our Selenium.

96
04:41.730 --> 04:43.410
However, you'll also notice

97
04:43.410 --> 04:46.560
that the Chrome browser is also closed immediately

98
04:46.560 --> 04:48.990
after our program finishes running.

99
04:48.990 --> 04:51.180
So in order to keep the browser open

100
04:51.180 --> 04:52.950
after our program finishes

101
04:52.950 --> 04:55.590
we have to configure our webdriver.

102
04:55.590 --> 04:59.160
So to do that, we have to get hold of the Chrome options.

103
04:59.160 --> 05:03.010
We can get those via webdriver.chrome options

104
05:04.890 --> 05:08.040
and then we have to add an experimental_option in Chrome

105
05:08.040 --> 05:11.133
called detach and set that option to true.

106
05:13.500 --> 05:15.540
Now we pass that configuration over

107
05:15.540 --> 05:18.753
to our webdriver via parameter called options.

108
05:19.860 --> 05:22.170
And now when we run our Python program,

109
05:22.170 --> 05:23.190
our Chrome browser

110
05:23.190 --> 05:25.713
doesn't close itself automatically anymore.

111
05:27.450 --> 05:30.660
So we've looked at configuring our browser to keep it open

112
05:30.660 --> 05:32.850
but we can also close the tabs

113
05:32.850 --> 05:35.910
and quit Chrome programmatically.

114
05:35.910 --> 05:39.510
To do that, all we have to do is tap into driver

115
05:39.510 --> 05:42.180
and then we can call the method close().

116
05:42.180 --> 05:44.340
Now, when I hit the Run button,

117
05:44.340 --> 05:47.760
you can see now it's going to open up amazon.com, and

118
05:47.760 --> 05:49.740
as soon as that page loaded

119
05:49.740 --> 05:52.380
it pretty much just closed it down.

120
05:52.380 --> 05:57.380
Now there's also another method called driver.quit().

121
05:57.570 --> 05:59.700
Now what's the difference between close and quit,

122
05:59.700 --> 06:00.600
you might wonder.

123
06:00.600 --> 06:04.590
Well, close actually just closes a single tab,

124
06:04.590 --> 06:08.760
the active tab where you've opened up a particular page.

125
06:08.760 --> 06:11.790
Now quit is actually going to quit the entire browser.

126
06:11.790 --> 06:13.950
So there are certain cases where you have

127
06:13.950 --> 06:16.980
more than one tab running at the same time,

128
06:16.980 --> 06:19.380
close() will close that particular tab,

129
06:19.380 --> 06:22.860
while quit() will actually quit the entire program.

130
06:22.860 --> 06:25.050
Once I'm done using my driver,

131
06:25.050 --> 06:27.360
I prefer actually just using quit()

132
06:27.360 --> 06:30.570
to shut down the entire browser.

133
06:30.570 --> 06:32.280
That way we can always start again

134
06:32.280 --> 06:36.450
from scratch and we'll have a fresh browser to work with.

135
06:36.450 --> 06:38.520
And in the next and coming lessons

136
06:38.520 --> 06:40.620
we're going to show you how to use Selenium

137
06:40.620 --> 06:42.960
to do basically anything that we could do

138
06:42.960 --> 06:45.300
with a mouse and keyboard as humans

139
06:45.300 --> 06:47.280
but instead we're going to automate

140
06:47.280 --> 06:49.980
the entire process and workflow.

141
06:49.980 --> 06:52.020
So for all of that and more,

142
06:52.020 --> 06:53.620
I'll see you on the next lesson.