Now.
In the last lesson, we looked at various ways of finding and locating elements on a particular HTML
page.
Now, in this lesson, we're going to put all of that to practice, and you're going to get hold of
all of these upcoming events.
Now, because these events are time dependent, the events that you'll see will, of course, be different
from what I've got here.
But the idea is to get hold of all of these dates.
So all five of these, and then get hold of all five of these names.
And we're going to create a dictionary from these events.
And by the end of the challenge, you should be able to print out a dictionary that structured like
this.
It's going to contain five items starting from zero.
So this is the first key.
And then the first value is a dictionary with a key of time and a key of name.
And then the values corresponds to of course the first date and the first name.
So the first one we've got here is PyCon Japan 2020.
And you can see down here in this section upcoming events.
The first one is this item.
So we're basically converting whatever is here in the upcoming events into this dictionary format.
That is the goal.
And you're going to be needing everything that you've learned previously.
In addition, it might be worth taking a look at the documentation for locating elements with selenium.
So I'll link to this page in the course resources as well.
And once you're ready, get inspecting and see if you can complete this challenge and print out this
dictionary.
Pause the video now.
So our goal is to get hold of this piece of data and this piece of data, but just the text we're going
to have to first figure out how to locate these items.
So if I go ahead and inspect on this date, you can see that it's inside a HTML element called time.
Now, in order to get hold of this element time, we can of course use an XPath.
But the problem is that this XPath will be specific for this first item.
While we actually probably want to use a find method that can find all of the dates and all of the event
names.
So we're going to be using one of these multiple element finds.
So where it says find elements.
Now I think the easiest way here at least for me, is to think about it in terms of CSS selectors.
This is a time element that lives inside a Li, which is inside a UL, but we still haven't found anything
that's unique to this particular structure here.
Because if you take a look over here when we look at the latest news.
It's also a time inside an ally inside a UL.
And none of this is unique.
Until we get to this div where we have a blog widget.
While here we've got a event widget.
So there finally is a unique class name.
We're going to use this class name and then find the time element.
Back in our code let's go ahead and tap into our driver and then use the find element by CSS selector,
which is going to give us a list of elements that match this selector.
And the selector is going to be first find a div with this particular class.
So it has to have the class event widget.
So we'll write dot event widget.
And then after space we specify the next thing we want to drill down to which is a time element.
So let's go ahead and just put the name of the HTML element like this.
And we'll get the event times as a list, hopefully.
So let's go ahead and print this out.
Event times.
And because this is actually going to be a selenium object rather than the actual text, we'll need
to use a for loop in order to actually see what it is.
So for time in event times, let's go ahead and print each of the time dot text.
So once selenium has done its thing, you can see it's now got hold of all five dates.
The next thing we need to do is to get hold of the event names.
And we're going to use a similar method.
So let's go ahead and inspect on the name.
And you can see that this is now a anchor tag.
Now you might think that the solution will be just as simple as copying what we had before and replacing
the time element with an anchor tag element.
But you'll see as I write my for loop forname in event names print name dot text.
This does not actually get us what we want, because it also gives us the first anchor tag in that div
with class name event widget, which is this more link here.
So if we don't want that more link, we're going to have to be a little bit more creative.
This anchor tag is also inside an Li.
While that more link is definitely not inside an Li, so we can narrow down on our selector by saying
okay, so it's inside a element with class event widget, but then it's inside an Li and then it's inside
an anchor tag.
Now that I've updated that CSS selector, you can see when I hit print it gets us the actual names of
all of the conferences.
So the final thing to do is to actually create our events dictionary.
You could do this using dictionary comprehension, but I'm going to do it in a slightly more long form
way, just so that anybody who's a little bit confused will make it a little bit easier to understand.
We're going to create a for loop and I'm going to use N.
So I'm going to say for n in range.
And the range is going to be from zero to the length of event times.
So it's basically going to be a range from 0 to 4.
Once I've got that range now I'm going to add to my event.
The key of the event is the actual n.
So the number and then the value of the event is a dictionary with the key of time, and also a key
of name like this.
The time is going to be from the event times and then getting hold of the item at index n and the name
is going to be event names and the item at index n.
The final thing we need to do is this gets hold of a selenium object, and we have to get hold of the
actual text.
So let's write dot text.
And now we can print our events dictionary.
And once that's done you can see this is in the exact format that we wanted.
We've got dictionary with five items.
Each item has a dictionary in itself.
Time and name of the upcoming Python conferences.
So did you manage to complete that challenge?
If not, it might be worth either reviewing CSS selectors, which we went through in previous lessons,
or reviewing some of the lessons previously where we discussed how to locate and how to get hold of
the text from elements using selenium.