Let's start getting into some interesting things we can do with selenium.
Now, what we're going to use for this exercise is going to be this website called Selenium, Etsy.com,
specifically the slash test part, because this website gives us a nice testing ground for us to play
with a website.
For example, if I do input forms here and do simple form demo.
I can actually use this website to test out typing stuff in showing message and seeing if we can do
this with just code.
So the first thing I'm going to do is copy this URL.
And in our case, we'll just pick input forms and simple form demo.
Later on, you can try it by yourself trying out different things.
But this tries to mimic most website functionalities like form inputs, tables, alerts and models and
allows you to test your code really nicely.
So let's copy this URL and the first thing I'm going to do is, well, we're automation testers.
We, we don't really manually type things in anymore.
We let Selenium do that for us.
So the first thing I'm going to do is say, Hey, Chrome browser, I want you to get this URL.
In my case, I'm going to say, Hey, this is the URL that I just copy it and paste it.
Chrome browser, just get that for me.
So let's run that code.
And check it out.
I've opened up the browser with selenium and it took me to the URL that I specified.
So most of the times this is going to be your first line where we go to a specific URL.
Now I want a window to be maximized.
So once again, we'll just say Chrome browser and I'll say maximize.
Window so that we'll have a full fledged window this time around.
Now I'm going to close this once again because every time we run this code, a new window is going to
open up.
So let's just close it for now.
Now, the next thing I want to do is to check that we're on the right page.
Now, the way Selenium works is that it's going to look at this page and try to grab elements or find
elements in this page, and it uses HTML that this page is built on to do this.
Now, HTML simply is if I right click on anything in the page and I click on Inspect.
A developer console is going to show up and in the Elements tab over here, it actually shows you how
the page is structured using HTML.
HTML is what allows web pages to get displayed in a browser, and they simply contain information about
each element.
In our case, see over here how it says complete automation testing tutorials.
HTML says, Hey, I want you to write this text right here.
So that let's say we go to the name and slogan or let's open up this.
We see that we have selenium made easy right here.
We have an image which is right over here.
Essentially, HTML allows us to display some text and some basic content on a website.
So with selenium, we're able to grab.
These elements and check them and modify them and do what we want.
Let's go all the way up and show you this.
Each HTML page always has a title tag.
And a title is simply what gets displayed up here at the top.
In our case, it says Selenium Easy Demo.
So what I can do is actually copy this.
And say, hey, in our code is the string selenium easy demo.
Is that in chrome browser dot title?
Now let's just print here and see what we get.
I'm going to run this code.
The browser opens up.
And if I go back to the code, look of that I get true because that text is in the title.
If I do something like Python Easy demo.
And I run this.
You'll see that I'll get false.
Now a easier way for us to check our code then.
Just running print all the time is to use the assert keyword that comes with Python.
Now you won't see this around too much because what's going to happen here is if we assert something,
if this returns false, it's going to actually error out and exit out of the code.
So if I run this.
You'll see that I get an error.
With an assertion error.
So this is a good way for us to test if something exists or not.
But let's go back to what we had before, which is Selenium Easy demo.
That should give us true and not error in our code.
Now, another thing here is we probably have a ton of windows here, so let's close them up.
And don't worry, I'll show you how to actually close these later on if we want.
So I showed you how I'm able to extract the title from here.
Another thing I can do is let's say, hey.
I see over here in my HTML that there's a head tag and then there's also a body tag.
So here could I do something like Hey assert.
Body.
If I click run here.
Hmm.
It looks like we're not able to grab body.
So what can we do here?
Well, another way for us to interact with a page like this is using selectors.
Now the selectors, I'll provide a link to some of the chichi here that we can use, but the selectors
are simply ways for us to grab items from a page.
We can grab items by ID, by name by class, name by tag, name by link text.
There's a ton of different things that we can do.
Now to show you over here how that would look like.
If I go to the HTML page over here, the elements, you'll see that if we wanted to grab, let's say
in our case.
We want to grab this button.
Well, I can right click, click inspect.
And see over here that I have the button has a type, it has a class and it has some text which is show
message.
If I go to the cheat sheet, I see here that I can grab things by different.
Different options.
We can do it by CCS.
We can do it by the link tax.
We can do by tag name.
We can do it by class name.
And you see here that it says driver find element by class name.
So this is how we can select it.
So let's do that.
I'm going to copy and paste this and simply say, Hey, Chrome browser, I want you to find element
by class name.
And what was the class name that we used?
Well, in our case, the class name is, let's say.
Bond default.
So I'm going to say BTN.
Default.
And this is going to be our button.
So if I run this and simply print button like this, if I run my code.
You'll see here that I'm able to grab from the Selenium Web driver a web element.
And it looks like it's an element.
I'm not really sure what's happening here, but we are grabbing something, a button.
Now, this button element that we've grabbed is exactly like we see it here.
If I again inspect the element, we've captured this part of the code.
So this selenium now contains this button for us to use.
And as a shortcut here you see over here it says equal equals dollar sign zero.
This actually allows us to select this element and use it in what we call the console.
So if I clear the console and just say dollar zero, you'll see that this is the element that we have
captured.
Now in here, there's different ways that we can interact with it.
One of the ways is to say something like Get.
Attribute.
And an attribute called inner HTML.
Now in HTML is simply going to grab whatever is between inside of this HTML, which is the button.
And then you can see over here that the button's closing.
So we want to grab the show message.
If I do something like this, let's say we call this button text and we print the button text.
If I run this.
Oh, I'll actually get an error, because you can see here that I did the syntax wrong.
I try to print the bun text and then got the attributes.
So instead make sure we move the brackets like this and run the code again.
There we go.
You see over here that we have show message.
So I was able to, through code, inspect the page.
Find out what I want to control in this case.
Maybe it's this button and I was able to grab the text using the get attribute method.
Again, all these methods I will link to.
They all are something that you just have to get used to when you get into automation.
But if you understand basic HTML, then they're quite easy to understand.
There's actually not a lot of them.
I would say there's probably a 20 or so really useful methods that you can learn and use.
But you know what?
Right now, this isn't that exciting.
I mean, yeah, sure.
We've opened up a browser.
We got to well, look at a button and display the text that it shows.
But I want to actually mimic something that we would do on a browser.
Let's say we had this input.
And I want to modify this a page where I enter some sort of text.
I click show message and just like, as if I'm signing into a website or uploading on Reddit.
Let's automate that in the next video.