In this demo,
we are going to be looking at how to code out the different
methods of locating elements on a web page,
the theory of which was discussed earlier.
We're back in the Jupyter Notebook, and as usual,
first we put all the necessary packages.
Then we add an option to Chrome to maximize the window size.
I added it for convenience in making the course.
You can choose to completely ignore it as it does not
provide any functional differences.
In case you do decide to go through,
note that there are differences in how different operating
systems and browsers treat certain arguments.
As such, not all of the arguments work across the board.
But this is fairly easy to get passed with some Googling.
Right, without further ado, let's move on to the first method,
locating elements by name.
What we have done in this code block is to go to the Pluralsight website,
locate the search bar, and type in my name and see what comes up.
Let's go step by step.
Here we instantiate the clone driver.
Remember, it's perfectly okay to go ahead without adding any options.
This line provides the address of the website we intend to visit.
Driver.get will navigate to that website.
WebDriver will wait until the page is fully loaded before
returning control to your test or script.
And now, we have to find the element using the name.
Let's see how to do that.
Let's open up the Pluralsight website on another tab,
right-click in the search bar, and click on Inspect.
As you can see in the highlighted text, the name here is q.
Let's now go back to the notebook.
The element can be located like this, driver.find_element_by_name q.
I'm giving it a little bit of time in the middle.
This, again,
is perfectly okay for you to ignore since it is only there to record the _____.
Next, we be sending keys.
This is similar to entering keys using your keyboard.
But first,
we make sure we clear any prepopulated input text in the search
bar so it doesn't affect our search results.
Here we simulate pressing the Return or Enter key using the keys class.
Let's execute that and see what happens.
Brilliant, it does exactly what we want it to do.
It navigates to the website, finds the search bar,
types in my name, and presses Enter.
Finally, we use driver.quit to close the browser.
In the next demo, we'll continue with the other methods of locating elements.