1
00:00:00,150 --> 00:00:07,740
In this video, you learn how to write a script which logs in into this form with a username and password.

2
00:00:09,130 --> 00:00:10,240
Just like I am doing.

3
00:00:15,780 --> 00:00:17,700
I'll give you the username and the password.

4
00:00:18,420 --> 00:00:19,800
Once the script logs in,

5
00:00:21,440 --> 00:00:24,500
then the script clicks on the home page here.

6
00:00:24,620 --> 00:00:29,990
So currently, it's in automated.pythonanywhere.com  dashboards and then clicks on the

7
00:00:29,990 --> 00:00:31,820
home page, and that's it.

8
00:00:32,119 --> 00:00:39,560
So filling the username and password data in the form, logging in and then pressing doing something

9
00:00:39,590 --> 00:00:43,160
on the other page, which is clicking this button here.

10
00:00:43,610 --> 00:00:44,810
So again, let me log out.

11
00:00:45,710 --> 00:00:46,940
This is the homepage now.

12
00:00:47,420 --> 00:00:56,420
So if we want to log in through a script, we want to give the script of the URL where the logging data

13
00:00:56,420 --> 00:00:57,470
should be entered.

14
00:00:57,830 --> 00:01:02,600
And I see that when I press that log in button, the URL is this one in here.

15
00:01:03,200 --> 00:01:10,130
So automated.pythonanywhere.com/login/. And then, the user name.

16
00:01:10,160 --> 00:01:17,260
Here is, as I said, automated and the password is automated

17
00:01:17,270 --> 00:01:19,010
automated.

18
00:01:20,030 --> 00:01:21,950
Or let me write it down here as well.

19
00:01:22,430 --> 00:01:24,200
So it's automatedautomated.

20
00:01:24,770 --> 00:01:28,910
Two times, no spaces, no dashes.

21
00:01:30,140 --> 00:01:30,600
Right.

22
00:01:31,530 --> 00:01:43,290
Let's go to our repl now and write that script I am going to fork the first ripple we created today.

23
00:01:45,300 --> 00:01:48,840
Log in and click.

24
00:01:52,470 --> 00:02:00,660
So in this repl now we have the get driver function, which creates a web driver and it returns

25
00:02:00,660 --> 00:02:07,440
that web driver, but currently that web driver is pointing to this homepage.

26
00:02:07,560 --> 00:02:08,820
We don't want the homepage.

27
00:02:08,820 --> 00:02:11,550
We said, we want this page.

28
00:02:12,060 --> 00:02:16,510
So the homepage was this, we want this and fill in this data.

29
00:02:16,800 --> 00:02:25,320
So I'm copying this URL, and I'll paste it in here inside the quotes like that's.

30
00:02:27,550 --> 00:02:33,670
Then what we do is we get that driver through here.

31
00:02:34,060 --> 00:02:41,950
So from the main function definition we call that get driver method to get access to that driver, which

32
00:02:41,950 --> 00:02:44,180
is pointing to that URL.

33
00:02:44,410 --> 00:02:53,560
And then what we did previously was getting an element out of that driver, which was that text.

34
00:02:53,890 --> 00:02:56,770
Remember, there were some text here, this text.

35
00:02:57,370 --> 00:02:58,960
Now we're doing something else.

36
00:02:58,960 --> 00:03:03,040
We are accessing these to input boxes.

37
00:03:03,340 --> 00:03:06,610
So I'm going to run, click here and go to inspect element.

38
00:03:07,060 --> 00:03:09,970
And now we could do something easier here.

39
00:03:11,770 --> 00:03:14,620
We don't have to look for the X path.

40
00:03:15,340 --> 00:03:24,430
We could use a more robust method, which is whenever we have an ID in an element, you see this id

41
00:03:24,430 --> 00:03:30,640
of input, which is equal to id_ username? Whenever an element has that,

42
00:03:30,670 --> 00:03:34,960
then we can use that because it's easier and it's more robust.

43
00:03:35,860 --> 00:03:37,180
So find element

44
00:03:37,240 --> 00:03:42,880
by id, value it was id_username.

45
00:03:43,660 --> 00:03:47,440
And what do we want to do with this element?

46
00:03:48,460 --> 00:03:57,190
We want to send keys, which means what keys do we want to send?

47
00:04:00,110 --> 00:04:11,690
Automated, so what's happening here is that senc_keys expects to get keyboard keys as input,

48
00:04:11,690 --> 00:04:19,279
so it could be an enter key, for example, but it could also be letters and numbers and symbols.

49
00:04:19,640 --> 00:04:27,540
So in this case, we're entering the key a, the key u, the key t, o, m, et cetera.

50
00:04:28,100 --> 00:04:31,880
So these keys will be typed in inside

51
00:04:31,880 --> 00:04:33,380
that user name box.

52
00:04:34,130 --> 00:04:36,200
So let's see what this will do.

53
00:04:36,840 --> 00:04:39,830
And let's check before we run.

54
00:04:39,980 --> 00:04:42,380
We don't need to return the element this time.

55
00:04:43,220 --> 00:04:44,900
We're not scraping.

56
00:04:44,900 --> 00:04:50,270
We're performing some actions and we don't even need a variable.

57
00:04:50,270 --> 00:04:52,640
So we don't need to assign that to a variable.

58
00:04:53,330 --> 00:05:00,410
Just leave it like that. Main is called here, so it should be fine to run it, run.

59
00:05:07,310 --> 00:05:16,550
The browser opens and the script visits the login page you saw that's automated was typed in inside

60
00:05:16,550 --> 00:05:17,450
that username.

61
00:05:17,480 --> 00:05:17,750
Yep.

62
00:05:18,080 --> 00:05:18,830
I hope you did.

63
00:05:19,370 --> 00:05:23,000
If not, try to go back a few seconds.

64
00:05:23,360 --> 00:05:25,280
So this was successful.

65
00:05:25,670 --> 00:05:29,990
And we got none as output because this function returns

66
00:05:29,990 --> 00:05:30,500
none.

67
00:05:30,770 --> 00:05:37,940
So whenever you don't have a return keyword here, which returns something, then implicitly Python

68
00:05:37,940 --> 00:05:41,790
will return a none object, which is fine in this case.

69
00:05:42,620 --> 00:05:44,210
So what is the next step?

70
00:05:44,690 --> 00:05:50,390
The next step is to do driver.find_element

71
00:05:51,450 --> 00:05:55,850
by again id with value...

72
00:05:56,330 --> 00:06:00,410
So this time should be the id of the password.

73
00:06:01,490 --> 00:06:03,290
Let's see what it is.

74
00:06:03,950 --> 00:06:05,690
So that was the username.

75
00:06:06,740 --> 00:06:07,790
Let's right

76
00:06:07,790 --> 00:06:09,380
click here and inspect element.

77
00:06:09,980 --> 00:06:14,300
So it will take us to the input HTML element for passwords.

78
00:06:14,600 --> 00:06:16,430
So it's id_password.

79
00:06:17,340 --> 00:06:21,980
Let's go here and say id_password.

80
00:06:23,940 --> 00:06:25,640
Send_keys.

81
00:06:26,340 --> 00:06:28,770
The password was

82
00:06:29,040 --> 00:06:30,270
automatedautomated.

83
00:06:31,910 --> 00:06:34,210
Like that. Let's try.

84
00:06:39,210 --> 00:06:40,860
Yep, so it was successful.

85
00:06:41,250 --> 00:06:50,550
But what I would suggest is to perhaps import time here and give it some time between the two operations,

86
00:06:50,850 --> 00:06:52,410
such as two seconds.

87
00:06:52,950 --> 00:06:59,040
So once the script answers the automated user name, it waits for two seconds and then it enters

88
00:06:59,040 --> 00:07:00,200
the password.

89
00:07:00,780 --> 00:07:05,070
So what's next? Once we enter a username and password?

90
00:07:05,640 --> 00:07:15,870
Usually what we do as humans is we press the enter key so let's press the enter key once we enter the

91
00:07:15,870 --> 00:07:16,650
password.

92
00:07:17,130 --> 00:07:28,330
So the way we could do that is by saying here, password + keys.return.

93
00:07:29,070 --> 00:07:30,060
What is keys?

94
00:07:30,450 --> 00:07:32,400
Keys is an object

95
00:07:32,400 --> 00:07:36,780
we need to import here. From selenium.

96
00:07:38,860 --> 00:07:50,860
Webdriver.common.keys import keys and return is one of those keys, which means the

97
00:07:50,860 --> 00:07:55,660
return of the enter key that you press and you log into a website.

98
00:07:56,810 --> 00:07:57,410
Let's try.

99
00:08:04,000 --> 00:08:06,400
Username, wait, passwords.

100
00:08:08,000 --> 00:08:16,070
And we are logged in. The window disappeared because there was nothing else before and after that.

101
00:08:17,270 --> 00:08:25,460
But if we want to make sure where we are after we log in, we could do something like driver.current_url.

102
00:08:26,120 --> 00:08:30,230
Current_url without parentheses.

103
00:08:30,410 --> 00:08:36,080
So, it's a property we don't have to call it, but we have to print that property to see the results

104
00:08:36,080 --> 00:08:36,350
here.

105
00:08:36,590 --> 00:08:37,370
So, run again.

106
00:08:43,090 --> 00:08:50,410
And we see that the url is slash dashboards, so which is the url that you get after you log in.

107
00:08:50,680 --> 00:08:53,800
So the logging was successful even though we don't see anything here.

108
00:08:54,550 --> 00:08:56,440
So what do we want to do next

109
00:08:58,450 --> 00:09:00,680
in that page which is...

110
00:09:01,790 --> 00:09:03,440
So let me log in as a human.

111
00:09:04,790 --> 00:09:06,140
It's this page here.

112
00:09:06,860 --> 00:09:09,590
We said that we want to click over this home button.

113
00:09:09,620 --> 00:09:09,950
Right?

114
00:09:10,310 --> 00:09:13,730
So we right-click there, we go to inspect elements.

115
00:09:14,540 --> 00:09:16,010
This is the home button.

116
00:09:16,730 --> 00:09:18,140
Does it have an ID?

117
00:09:18,170 --> 00:09:23,660
No, that means we need to work with the xpath.

118
00:09:23,760 --> 00:09:36,440
Right-click, go to copy and xpath and then driver.find_element

119
00:09:38,200 --> 00:09:43,660
bby xpath, the value of this is what we copied, which is this one.

120
00:09:44,500 --> 00:09:45,670
And we want to click that.

121
00:09:47,270 --> 00:09:53,660
And perhaps print driver.current_url like that.

122
00:09:54,830 --> 00:10:03,800
And I would prefer to give it some time. Time sleep. After we log in, we stay a bit like two seconds

123
00:10:03,980 --> 00:10:10,190
in the page of then we click the home button here right, run.

124
00:10:18,640 --> 00:10:20,230
And seems to be working.

125
00:10:21,160 --> 00:10:24,460
So we went to the home page, which is this one in here?

126
00:10:25,000 --> 00:10:32,530
So again, what happens is that after the login page, which is this one here, the scripts clicked on

127
00:10:32,530 --> 00:10:35,530
home and it went to the home page, which is this one in here?

128
00:10:36,040 --> 00:10:41,860
So the home page is slightly different than the home page you see when you are not loged in.

129
00:10:42,250 --> 00:10:43,380
So log out.

130
00:10:43,390 --> 00:10:48,220
This is the home page without the log in and it's slightly different.

131
00:10:48,340 --> 00:10:50,200
You see, we don't see the username here.

132
00:10:50,320 --> 00:10:59,590
When you log in and you enter as a user, you see you go to home and you see you have this automated

133
00:10:59,800 --> 00:11:00,640
username here.

134
00:11:01,060 --> 00:11:02,170
So that's the idea.

135
00:11:02,560 --> 00:11:04,800
I hope this is useful for you.

136
00:11:04,810 --> 00:11:06,460
This is the entire script.

137
00:11:08,940 --> 00:11:16,700
In the next videos, we're going to go even further and extract this value from this page after we have

138
00:11:16,710 --> 00:11:17,910
logged in, so see you.

