We've seen how to locate an element and how to enter
text to the field that supports it.
But there are other challenges to navigating web page,
such as forms, drop-down menus, and its help,
which is what we're going to tackle in this demo.
We're back inside the Jupyter Notebook,
and first we'll learn to deal with forms and drop-down menus.
For that, let's try to fill out the signing-up form in the Pluralsight website.
Let's first navigate to that page and see what we are up against.
We'll open up the Pluralsight website and click on Get Started.
We'll scroll down a bit and click on the Get Started button again,
under the monthly pricing scheme.
And this is the form we try to fill out.
You can see, it has a drop-down box under the country of residence category.
If you noticed,
there's the loading string that appears when we first navigated to this page.
We'll have to address that as well.
Let's right-click on first name field and click on Inspect.
You can see that this can be located using the name,
so can each of the other fields.
Let's get to coding them.
First, as usual, we'll import the required packages.
Then, we add the option for Chrome to be displayed in full string.
We instantiate the Chrome driver.
Then, we navigate to the relevant web page.
Here we copy/paste the address of the sign up page.
You're also welcome to try and navigate to the page using Selenium only.
After the previous demo, it shouldn't be that hard.
Then, we add a WebDriverWait.
This is where we take care of the loading screen.
This implements a wait of 10 seconds, within which the element becomes clickable,
then we proceed to the rest of the procedures.
This is important,
as not doing this will result in error messages which
point to the element not being detected.
Now, we locate each of the text fields by name,
clear the field, use send_keys to fill in the appropriate values.
We'll use an imaginary name and email ID.
And then we run this code block, you can see exactly what happens.
(Loading) Now let's see how we tackle the country of residence drop-down menu.
We'll assume our imaginary user is from the United States.
If you inspect the drop-down element,
then we can see we have short forms of all the
countries attached with the option tag, that's what we target.
First, we'll locate the drop-down menu element using XPath.
Then we'll use locate elements by tag method to locate the option tags.
Here do note that we use find elements, not element, but elements.
This will mean that all option tags will be returned as a list.
Then we use a for loop to locate the option tag,
which points to US, and then click on that.
After that, we locate the checkbox using XPath and then click on that.
We do the same thing with submit button.
Running this code block should help us successfully complete this form.
(Loading) Now, let's go through other methods of navigating a web page.
I won't be getting into the details of it,
but will be giving you a quick overview.
Handling iFrames, what is an iFrame?
IFrame, or inline frame,
is an HTML document embedded inside another HTML document on a website.
The iFrame HTML element is often used to insert content from another source,
such as advertisement, videos, etc., into a web page.
Learning to switch between multiple iFrames inside a web page is
needed to access elements inside the iFrame.
There are many methods to switch to your desired iFrame.
One of them is by frameName, as shown here.
This line shows you how to switch back to the parent frame.
And finally, this is how you handle those annoying,
or in some cases useful, popups.
This line will help you access the popup and will
return the currently open popup object.
With this object, we can now accept, dismiss, or read its contents.
This interface works equally well on alerts, confirms, and prompts.