1
00:00:00,330 --> 00:00:03,690
Instructor: In order to start using Selenium Webdriver,

2
00:00:03,690 --> 00:00:04,830
we're going to have to go through

3
00:00:04,830 --> 00:00:08,340
a number of steps in order to set up our browser

4
00:00:08,340 --> 00:00:11,610
and also to install and set up Selenium.

5
00:00:11,610 --> 00:00:14,190
Now, the first step, if you haven't already

6
00:00:14,190 --> 00:00:17,100
is to install the Chrome browser.

7
00:00:17,100 --> 00:00:20,370
Now, while you can work with Selenium using other browsers

8
00:00:20,370 --> 00:00:24,270
like Firefox or Safari, we're going to be using a number

9
00:00:24,270 --> 00:00:26,760
of Chrome developer tools and also in order

10
00:00:26,760 --> 00:00:30,210
for you to work alongside to do exactly what I'm doing

11
00:00:30,210 --> 00:00:31,860
it'll be much easier if you're working

12
00:00:31,860 --> 00:00:34,650
on the same browser and the same technology.

13
00:00:34,650 --> 00:00:36,660
If you haven't already got Chrome than head over

14
00:00:36,660 --> 00:00:39,330
to the course resources where you'll find a link

15
00:00:39,330 --> 00:00:43,260
to download the latest version for your computer.

16
00:00:43,260 --> 00:00:45,780
So now that we've installed Chrome

17
00:00:45,780 --> 00:00:50,190
the next and last step is to install and set up Selenium.

18
00:00:50,190 --> 00:00:53,680
In my main.py, I'm going to import Selenium

19
00:00:54,780 --> 00:00:57,030
and make sure that you spelt it correctly

20
00:00:57,030 --> 00:00:58,830
so it looks like this.

21
00:00:58,830 --> 00:00:59,820
And then we're gonna click

22
00:00:59,820 --> 00:01:04,473
on this red light bulb as always to install our package.

23
00:01:05,820 --> 00:01:09,210
Now, once Selenium has been installed successfully

24
00:01:09,210 --> 00:01:11,610
I'm going to change the import statement.

25
00:01:11,610 --> 00:01:12,660
I'm going to say that

26
00:01:12,660 --> 00:01:16,290
from this newly installed Selenium package

27
00:01:16,290 --> 00:01:20,490
I'm going to import a module called webdriver

28
00:01:20,490 --> 00:01:22,320
and it's this web driver

29
00:01:22,320 --> 00:01:25,170
which is going to be driving the Chrome browser

30
00:01:25,170 --> 00:01:28,290
and doing all of our automated tasks.

31
00:01:28,290 --> 00:01:33,290
Let's create a new driver from that new module.

32
00:01:33,450 --> 00:01:35,043
So driver = webdriver.

33
00:01:36,930 --> 00:01:40,650
and then we can now instantiate any browser of our choice.

34
00:01:40,650 --> 00:01:43,950
So in our case we're gonna be creating a Chrome browser

35
00:01:43,950 --> 00:01:47,760
to drive but you can see that you can also have Firefox

36
00:01:47,760 --> 00:01:51,333
or Safari and a large number of other browsers.

37
00:01:52,320 --> 00:01:54,210
We're going to choose Chrome

38
00:01:54,210 --> 00:01:57,000
and notice that this is Chrome with a capital C.

39
00:01:57,000 --> 00:01:59,343
So we're initializing a new object here.

40
00:02:00,210 --> 00:02:02,400
And now that we've created our driver

41
00:02:02,400 --> 00:02:03,660
we're pretty much done

42
00:02:03,660 --> 00:02:07,020
with installing and setting up Selenium Webdriver.

43
00:02:07,020 --> 00:02:09,660
But if you're wondering what this Chrome driver is,

44
00:02:09,660 --> 00:02:11,640
well here's the way to think about it.

45
00:02:11,640 --> 00:02:15,120
We've got our Selenium package, which contains code

46
00:02:15,120 --> 00:02:17,970
for us to be able to interact with browsers.

47
00:02:17,970 --> 00:02:20,460
Now, it can interact with the Chrome browser

48
00:02:20,460 --> 00:02:22,470
which is what we're choosing to do

49
00:02:22,470 --> 00:02:23,670
but it can also interact

50
00:02:23,670 --> 00:02:27,330
with a bunch of other ones like Safari or Firefox.

51
00:02:27,330 --> 00:02:30,150
Now, how do we make sure that this package

52
00:02:30,150 --> 00:02:32,280
which can handle all three

53
00:02:32,280 --> 00:02:35,523
knows specifically how to work with the Chrome browser?

54
00:02:36,750 --> 00:02:38,250
Well, we're gonna need some sort

55
00:02:38,250 --> 00:02:43,250
of a bridge that bridges the Selenium code to work

56
00:02:43,770 --> 00:02:46,050
with the Chrome browser.

57
00:02:46,050 --> 00:02:50,160
And this bridge is provided by the Chrome driver.

58
00:02:50,160 --> 00:02:51,720
So there'll be a different driver

59
00:02:51,720 --> 00:02:55,140
for Safari and there'll be a different driver for Firefox.

60
00:02:55,140 --> 00:02:57,810
And just by switching up those drivers

61
00:02:57,810 --> 00:02:59,790
it'll tell Selenium how to work

62
00:02:59,790 --> 00:03:02,730
with the latest version of these browsers.

63
00:03:02,730 --> 00:03:05,430
All we have to do is now use it.

64
00:03:05,430 --> 00:03:07,230
So the first thing I'm gonna use my driver

65
00:03:07,230 --> 00:03:12,180
to do is I'm just going to get it to open up a webpage.

66
00:03:12,180 --> 00:03:13,830
We'll use driver.get

67
00:03:13,830 --> 00:03:16,980
and then we can pass in the URL here.

68
00:03:16,980 --> 00:03:19,020
It's going to be https

69
00:03:19,020 --> 00:03:22,590
and then I'm going to go to amazon.com.

70
00:03:22,590 --> 00:03:26,340
Now if you're on Windows and you go ahead and run the code

71
00:03:26,340 --> 00:03:28,800
then it should run without any issues.

72
00:03:28,800 --> 00:03:31,563
But if you're on a Mac, this is what happens.

73
00:03:33,600 --> 00:03:36,960
You'll get a popup that says Chrome driver cannot be opened

74
00:03:36,960 --> 00:03:39,750
because the developer cannot be verified

75
00:03:39,750 --> 00:03:41,910
even though we know that this is actually created

76
00:03:41,910 --> 00:03:45,750
by Google specifically for the Chrome browser.

77
00:03:45,750 --> 00:03:46,800
So if this happens,

78
00:03:46,800 --> 00:03:49,260
you're gonna click cancel and you'll get all

79
00:03:49,260 --> 00:03:53,100
of these errors because it's unable to access that file.

80
00:03:53,100 --> 00:03:54,570
Now, in order to enable it

81
00:03:54,570 --> 00:03:58,120
we're going to go to the Apple symbol system preferences

82
00:03:59,850 --> 00:04:02,610
and then we're gonna go to security and privacy.

83
00:04:02,610 --> 00:04:05,520
And here, because we tried to run it before,

84
00:04:05,520 --> 00:04:07,050
we've got something that says

85
00:04:07,050 --> 00:04:09,360
Chrome driver was blocked from use.

86
00:04:09,360 --> 00:04:12,570
So we're gonna click on the allow anyways.

87
00:04:12,570 --> 00:04:16,950
And now if we run this again, then it'll give us this popup

88
00:04:16,950 --> 00:04:20,070
and all we have to do is click open.

89
00:04:20,070 --> 00:04:22,560
And once we've done all of that,

90
00:04:22,560 --> 00:04:26,280
now it's going to finally open up the site

91
00:04:26,280 --> 00:04:28,920
that we wanted to go to amazon.com

92
00:04:28,920 --> 00:04:32,640
using this automated version of Chrome.

93
00:04:32,640 --> 00:04:35,820
And you can see this because at the right at the top here

94
00:04:35,820 --> 00:04:37,770
it says Chrome is being controlled

95
00:04:37,770 --> 00:04:39,510
by automated test software.

96
00:04:39,510 --> 00:04:41,730
That is our Selenium.

97
00:04:41,730 --> 00:04:43,410
However, you'll also notice

98
00:04:43,410 --> 00:04:46,560
that the Chrome browser is also closed immediately

99
00:04:46,560 --> 00:04:48,990
after our program finishes running.

100
00:04:48,990 --> 00:04:51,180
So in order to keep the browser open

101
00:04:51,180 --> 00:04:52,950
after our program finishes

102
00:04:52,950 --> 00:04:55,590
we have to configure our web driver.

103
00:04:55,590 --> 00:04:59,160
So to do that, we have to get hold of the Chrome options.

104
00:04:59,160 --> 00:05:03,010
We can get those via webdriver.chrome options

105
00:05:04,890 --> 00:05:08,040
and then we have to add an experimental option in Chrome

106
00:05:08,040 --> 00:05:11,133
called detach and set that option to true.

107
00:05:13,500 --> 00:05:15,540
Now we pass that configuration over

108
00:05:15,540 --> 00:05:18,753
to our web driver via parameter called options.

109
00:05:19,860 --> 00:05:22,170
And now when we run our Python program,

110
00:05:22,170 --> 00:05:23,190
our Chrome browser

111
00:05:23,190 --> 00:05:25,713
doesn't close itself automatically anymore.

112
00:05:27,450 --> 00:05:30,660
So we've looked at configuring our browser to keep it open

113
00:05:30,660 --> 00:05:32,850
but we can also close the tabs

114
00:05:32,850 --> 00:05:35,910
and quit Chrome programmatically.

115
00:05:35,910 --> 00:05:39,510
To do that, all we have to do is tap into driver

116
00:05:39,510 --> 00:05:42,180
and then we can call the method close.

117
00:05:42,180 --> 00:05:44,340
Now, when I hit the run button

118
00:05:44,340 --> 00:05:47,760
you can see now it's gonna open up amazon.com.

119
00:05:47,760 --> 00:05:49,740
as soon as that page loaded

120
00:05:49,740 --> 00:05:52,380
it pretty much just closed it down.

121
00:05:52,380 --> 00:05:57,380
Now there's also another method called driver.quit.

122
00:05:57,570 --> 00:05:59,700
Now what's the difference between close and quit,

123
00:05:59,700 --> 00:06:00,600
you might wonder.

124
00:06:00,600 --> 00:06:04,590
Well, close actually just closes a single tab,

125
00:06:04,590 --> 00:06:08,760
the active tab where you've opened up a particular page.

126
00:06:08,760 --> 00:06:11,790
Now quit is actually gonna quit the entire browser.

127
00:06:11,790 --> 00:06:13,950
So there are certain cases where you have

128
00:06:13,950 --> 00:06:16,980
more than one tab running at the same time.

129
00:06:16,980 --> 00:06:19,380
Close will close that particular tab

130
00:06:19,380 --> 00:06:22,860
while quit will actually quit the entire program.

131
00:06:22,860 --> 00:06:25,050
Once I'm done using my driver,

132
00:06:25,050 --> 00:06:27,360
I prefer actually just using quit

133
00:06:27,360 --> 00:06:30,570
to shut down the entire browser.

134
00:06:30,570 --> 00:06:32,280
That way we can always start again

135
00:06:32,280 --> 00:06:36,450
from scratch and we'll have a fresh browser to work with.

136
00:06:36,450 --> 00:06:38,520
And in the next and coming lessons

137
00:06:38,520 --> 00:06:40,620
we're gonna show you how to use Selenium

138
00:06:40,620 --> 00:06:42,960
to do basically anything that we could do

139
00:06:42,960 --> 00:06:45,300
with a mouse and keyboard as humans

140
00:06:45,300 --> 00:06:47,280
but instead we're going to automate

141
00:06:47,280 --> 00:06:49,980
the entire process and workflow.

142
00:06:49,980 --> 00:06:52,020
So for all of that and more,

143
00:06:52,020 --> 00:06:53,620
I'll see you on the next lesson.

