WEBVTT 1 00:00:00.160 --> 00:00:01.530 Hi, guys, and welcome back. 2 00:00:01.530 --> 00:00:03.862 In this video, we're going to be encapsulating our logic 3 00:00:03.862 --> 00:00:08.260 into our page so that it the app dot py becomes much simpler 4 00:00:08.260 --> 00:00:11.363 and we can very easily reproduce a search. 5 00:00:12.510 --> 00:00:16.312 I'm gonna start off at app dot py again by asking the user 6 00:00:16.312 --> 00:00:21.312 what author they want and what tag they want. 7 00:00:23.920 --> 00:00:26.480 Now, I'm no longer going to be showing the user what tags 8 00:00:26.480 --> 00:00:28.440 there are available because I want to run 9 00:00:28.440 --> 00:00:30.016 through the search very quickly 10 00:00:30.016 --> 00:00:34.060 so this is just an alternative way of doing things. 11 00:00:34.060 --> 00:00:36.747 Instead of getting the available tags and printing that, 12 00:00:36.747 --> 00:00:39.070 we're going to just delete that, we're gonna delete that, 13 00:00:39.070 --> 00:00:40.210 we're gonna delete that, 14 00:00:40.210 --> 00:00:43.560 and we're left with the central part of the logic 15 00:00:43.560 --> 00:00:46.410 which is select an author, select a tag, 16 00:00:46.410 --> 00:00:47.943 and click the search button. 17 00:00:48.920 --> 00:00:52.270 So we're going to cut that and we're gonna 18 00:00:52.270 --> 00:00:57.270 instead do page dot search for quotes, author, and tag 19 00:00:58.190 --> 00:01:00.060 or select a tag rather. 20 00:01:00.060 --> 00:01:01.660 I'm just gonna rename it to tag. 21 00:01:02.500 --> 00:01:05.130 Now, we're also going to print this 22 00:01:05.130 --> 00:01:07.670 because we wanna be as simple as possible. 23 00:01:07.670 --> 00:01:11.580 So what this is gonna do is something slightly different. 24 00:01:11.580 --> 00:01:15.767 It's no longer saying, hey, I wanna do this with the page. 25 00:01:15.767 --> 00:01:18.920 Now this is saying I wanna use this page to search 26 00:01:18.920 --> 00:01:20.440 for quotes and I don't actually care 27 00:01:20.440 --> 00:01:22.227 what happens to the page itself. 28 00:01:22.227 --> 00:01:24.930 This is a slightly different way of going 29 00:01:24.930 --> 00:01:27.460 about interacting with the page using selenium 30 00:01:27.460 --> 00:01:28.910 but it's a perfectly valid way 31 00:01:28.910 --> 00:01:31.120 and something I'll encourage you to do. 32 00:01:31.120 --> 00:01:34.010 If you can encapsulate actions within the page, 33 00:01:34.010 --> 00:01:37.350 it's gonna make using the page very easy. 34 00:01:37.350 --> 00:01:40.420 So going over to quotes page now down here, 35 00:01:40.420 --> 00:01:42.662 we're gonna add our method 36 00:01:42.662 --> 00:01:47.650 and that is going to be search for quotes. 37 00:01:47.650 --> 00:01:50.150 Takes in an author name which a string and a tag name 38 00:01:50.150 --> 00:01:51.920 which is a string and it's gonna return 39 00:01:51.920 --> 00:01:54.823 a list of quote parser. 40 00:01:55.760 --> 00:01:58.030 Okay, the first thing it's gonna do is 41 00:01:58.030 --> 00:02:03.030 is going to do self select author, author name. 42 00:02:03.130 --> 00:02:05.974 So just gonna call this method here. 43 00:02:05.974 --> 00:02:09.710 Then, it's going to try to do something 44 00:02:09.710 --> 00:02:13.560 and what it's going to try to do is select a tag 45 00:02:13.560 --> 00:02:15.600 and that's gonna be the tag name. 46 00:02:15.600 --> 00:02:19.290 But it's possible that it won't find it. 47 00:02:19.290 --> 00:02:22.620 No such element exception. 48 00:02:22.620 --> 00:02:25.957 And this one here has to be imported by the way 49 00:02:25.957 --> 00:02:30.957 from selenium so import selenium and there you go. 50 00:02:31.950 --> 00:02:35.090 So you wanna do from selenium common exceptions, 51 00:02:35.090 --> 00:02:37.563 import no such element exception. 52 00:02:37.563 --> 00:02:42.563 If it can't find it, that means that this tag didn't exist 53 00:02:43.390 --> 00:02:48.250 for this author so we're gonna raise an invalid tag 54 00:02:48.250 --> 00:02:52.150 for author error and finally if that doesn't happen, 55 00:02:52.150 --> 00:02:55.530 it's gonna do self dot search button, click, 56 00:02:55.530 --> 00:02:57.878 and then return self dot quote. 57 00:02:57.878 --> 00:03:02.723 Now we do need to have this invalid tag for author error 58 00:03:02.723 --> 00:03:05.840 which is going to be a value error subclass 59 00:03:05.840 --> 00:03:07.838 and it's not going to do anything. 60 00:03:07.838 --> 00:03:12.063 Okay, and capital F, there you go. 61 00:03:12.063 --> 00:03:15.300 So, went a bit quickly there but all this is doing 62 00:03:15.300 --> 00:03:17.960 is selecting and author, then selecting a tag, 63 00:03:17.960 --> 00:03:20.270 then clicking the button, and then returning quotes. 64 00:03:20.270 --> 00:03:21.890 And notice that all this is doing 65 00:03:21.890 --> 00:03:24.190 is it's using other methods and properties 66 00:03:24.190 --> 00:03:27.820 that we have previously defined and used in app dot py. 67 00:03:27.820 --> 00:03:31.377 This is a very natural progression for your applications. 68 00:03:31.377 --> 00:03:34.160 First, you define your application as doing something 69 00:03:34.160 --> 00:03:36.810 and then you encapsulate that application logic 70 00:03:36.810 --> 00:03:38.670 into a method or a function. 71 00:03:38.670 --> 00:03:41.112 Here, pulling it inside this page makes sense 72 00:03:41.112 --> 00:03:44.425 because it modifies and interacts with the page itself. 73 00:03:44.425 --> 00:03:48.040 By allowing the page to give us everything that you need 74 00:03:48.040 --> 00:03:49.827 in order to search and find quotes, 75 00:03:49.827 --> 00:03:52.575 it means that your apps become much simpler 76 00:03:52.575 --> 00:03:55.390 and also the page can still work as normal 77 00:03:55.390 --> 00:03:57.146 using the other methods if you want. 78 00:03:57.146 --> 00:04:00.899 We've added some error handling with this try, except 79 00:04:00.899 --> 00:04:03.710 just in case that this tag name is invalid 80 00:04:03.710 --> 00:04:05.280 because we're no longer telling the user 81 00:04:05.280 --> 00:04:09.330 what tags are valid so doing that will raise an error 82 00:04:09.330 --> 00:04:11.083 if the tag doesn't exist. 83 00:04:12.540 --> 00:04:15.110 Let's go over to app dot py and run it. 84 00:04:15.110 --> 00:04:17.290 And now you'll see that we've done another improvement. 85 00:04:17.290 --> 00:04:20.460 We ask for the author and the tag first 86 00:04:20.460 --> 00:04:23.310 and then we do the slow thing, which is launch in chrome. 87 00:04:23.310 --> 00:04:26.873 So we're gonna do Bob Marley and music. 88 00:04:28.050 --> 00:04:31.360 And now here comes chrome and everything happens instantly. 89 00:04:31.360 --> 00:04:35.920 It's now waiting for us to type so there you have 90 00:04:35.920 --> 00:04:38.767 your programme and then you have your quote. 91 00:04:38.767 --> 00:04:42.320 Because this application only ever prints out one quote 92 00:04:42.320 --> 00:04:45.810 for this tag, you get a list of one element 93 00:04:45.810 --> 00:04:48.760 but if there were more, you would get a list of all quotes. 94 00:04:49.970 --> 00:04:53.580 This is how we can encapsulate some logic into our page 95 00:04:53.580 --> 00:04:56.680 and this is really helpful to simplify your applications. 96 00:04:56.680 --> 00:04:58.070 Thanks for joining me in this video 97 00:04:58.070 --> 00:04:59.720 and I'll see you in the next one.