WEBVTT 1 00:00:00.090 --> 00:00:00.923 Hi guys 2 00:00:00.923 --> 00:00:01.756 and welcome back. 3 00:00:01.756 --> 00:00:03.100 In this video, we're going to be 4 00:00:03.100 --> 00:00:05.760 interacting with our first drop down. 5 00:00:05.760 --> 00:00:08.730 And in Selenium, they're called selects. 6 00:00:08.730 --> 00:00:11.160 Let's go over to our quotes page 7 00:00:11.160 --> 00:00:12.670 and add a couple of imports. 8 00:00:12.670 --> 00:00:15.740 The first one is gonna be just from the typing module, 9 00:00:15.740 --> 00:00:20.740 and the second one is from Selenium web driver support UI. 10 00:00:20.830 --> 00:00:22.480 And we're going to import select. 11 00:00:23.520 --> 00:00:27.250 The typing one is just so that we can do this 12 00:00:28.370 --> 00:00:30.600 in there, which was missing. 13 00:00:30.600 --> 00:00:32.650 And then we're gonna add another property 14 00:00:33.670 --> 00:00:36.790 which is for the author dropdown. 15 00:00:36.790 --> 00:00:41.790 And this is going to return a Select Object from Selenium. 16 00:00:42.210 --> 00:00:43.130 And what it's gonna do is 17 00:00:43.130 --> 00:00:44.430 it's going to first of all find this. 18 00:00:44.430 --> 00:00:45.653 So self dot browser, 19 00:00:46.760 --> 00:00:49.280 dot find element by CSS selector, 20 00:00:49.280 --> 00:00:54.120 and this is the quotes page locators dot author drop down. 21 00:00:54.120 --> 00:00:55.390 And then we're just going to return 22 00:00:55.390 --> 00:00:57.980 a Select of that element. 23 00:00:57.980 --> 00:00:59.720 What that does in Selenium terms, 24 00:00:59.720 --> 00:01:02.450 is it takes whatever arbitrary 25 00:01:02.450 --> 00:01:04.810 or generic Selenium element you've got, 26 00:01:04.810 --> 00:01:08.430 and it tries to put it inside a select wrapper. 27 00:01:08.430 --> 00:01:11.120 And what that does is it gives that element, 28 00:01:11.120 --> 00:01:14.760 some methods that we can use to interact with a Select. 29 00:01:14.760 --> 00:01:15.700 Now, you better hope 30 00:01:15.700 --> 00:01:18.780 that the element you're passing into this is a drop down. 31 00:01:18.780 --> 00:01:20.950 If it's not, you're gonna have some problems. 32 00:01:20.950 --> 00:01:22.990 And so now that we have the element there, 33 00:01:22.990 --> 00:01:26.300 that lets us create something like select author. 34 00:01:26.300 --> 00:01:27.930 And that takes in an author name, 35 00:01:27.930 --> 00:01:29.520 which is going to be a string, 36 00:01:29.520 --> 00:01:32.363 and that is going to do self dot author dropdown, 37 00:01:33.560 --> 00:01:35.860 which is these property here. 38 00:01:35.860 --> 00:01:39.810 Don't select by visible text. 39 00:01:39.810 --> 00:01:41.860 This is one of these methods that you get 40 00:01:41.860 --> 00:01:43.690 when you make a select 41 00:01:43.690 --> 00:01:47.280 and select by visible text is going to be author name. 42 00:01:47.280 --> 00:01:48.980 What that does in Selenium terms is 43 00:01:48.980 --> 00:01:51.470 it opens the drop down, sees what's there 44 00:01:51.470 --> 00:01:52.950 and then selects the one that 45 00:01:52.950 --> 00:01:55.380 matches this string if it's there. 46 00:01:55.380 --> 00:01:56.890 So that is how we're going to 47 00:01:56.890 --> 00:01:59.520 select an author from the drop down. 48 00:01:59.520 --> 00:02:01.090 Let's go over to App dot py 49 00:02:01.090 --> 00:02:03.740 and modify this code so that it actually does that. 50 00:02:03.740 --> 00:02:06.880 So let's first of all ask the user 51 00:02:08.270 --> 00:02:10.180 what author they want. 52 00:02:10.180 --> 00:02:13.750 Enter the author you'd like quotes from. 53 00:02:13.750 --> 00:02:16.950 And then we're going to do page dot select author, 54 00:02:16.950 --> 00:02:17.903 with that author. 55 00:02:18.840 --> 00:02:21.580 All right, so making sure that we've got everything there. 56 00:02:21.580 --> 00:02:23.810 And it seems like we do. 57 00:02:23.810 --> 00:02:24.643 Go ahead 58 00:02:24.643 --> 00:02:25.730 and run this. 59 00:02:25.730 --> 00:02:27.380 And chrome will open first because 60 00:02:27.380 --> 00:02:29.430 that's that line five there. 61 00:02:29.430 --> 00:02:31.280 Then we get asked what author we'd like 62 00:02:31.280 --> 00:02:33.750 so we're going to say something like Bob Marley 63 00:02:33.750 --> 00:02:35.790 just because his name is quite short. 64 00:02:35.790 --> 00:02:37.913 And then we have our first bug. 65 00:02:38.870 --> 00:02:40.530 You can see that the error 66 00:02:40.530 --> 00:02:44.610 says attribute error this object has no attribute tack name. 67 00:02:44.610 --> 00:02:47.640 That suggests that we've tried to do something on an object, 68 00:02:47.640 --> 00:02:50.290 but that thing was a list instead of 69 00:02:50.290 --> 00:02:51.810 whatever we were expecting. 70 00:02:51.810 --> 00:02:53.410 So something comes to mind immediately 71 00:02:53.410 --> 00:02:54.950 when something like this happens. 72 00:02:54.950 --> 00:02:59.610 And that is that we use the wrong method. 73 00:02:59.610 --> 00:03:01.940 And indeed we did for the author drop down, 74 00:03:01.940 --> 00:03:05.840 we said self dot browser dot find elements by CSS selector, 75 00:03:05.840 --> 00:03:08.490 you want find elements because there's only going to be one 76 00:03:08.490 --> 00:03:10.900 and you want just a single element, 77 00:03:10.900 --> 00:03:12.680 so that you can make a select out of it. 78 00:03:12.680 --> 00:03:14.530 You don't want multiple elements, 79 00:03:14.530 --> 00:03:16.590 or at least offer one element, 80 00:03:16.590 --> 00:03:18.880 because then it will be very confused. 81 00:03:18.880 --> 00:03:20.963 Okay, let's run app dot py again. 82 00:03:21.830 --> 00:03:24.870 Notice that this launches a completely new chrome instance 83 00:03:24.870 --> 00:03:25.920 when you do that, 84 00:03:25.920 --> 00:03:28.923 so you can end up with a bunch of different Chrome's. 85 00:03:31.330 --> 00:03:32.323 There you have it. 86 00:03:33.410 --> 00:03:36.680 And Bob Marley has been selected in our drop down. 87 00:03:36.680 --> 00:03:37.960 If you read the error messages you can 88 00:03:37.960 --> 00:03:42.000 try to glean something about Selenium's internal workings. 89 00:03:42.000 --> 00:03:42.900 But it can be tricky. 90 00:03:42.900 --> 00:03:45.830 If you do have any problems, ask away in the course Q&A 91 00:03:45.830 --> 00:03:47.940 or in the Slack channel, we'll be more than happy to help. 92 00:03:47.940 --> 00:03:49.430 Just make sure you give us your code 93 00:03:49.430 --> 00:03:51.550 and you know, as much information as you can, 94 00:03:51.550 --> 00:03:53.810 so that we can help you better. 95 00:03:53.810 --> 00:03:57.390 That's how you interact with one select or one drop down. 96 00:03:57.390 --> 00:04:00.030 I'm sure you can do the same for the tag name, 97 00:04:00.030 --> 00:04:02.150 but we're going to do that in the next video. 98 00:04:02.150 --> 00:04:03.100 I'll see you there.