Use the HTML name attribute to locate elements in Selenium.
- [Instructor] We can also look at elements using the name attribute. Let's take a look at this along with the code example. The HTML name attribute specifies a name for an HTML element. It does not have to be unique in a document and the name attribute is usually used in forms to reference the element when the data is submitted. This is commonly used for logging into a website with a username and password.
We use this method to look at elements when the name attribute of an element is known. In the case that there are multiple elements with the same name, the first element with the matching attribute value will be returned. Let's head over to the HTML webpage that we used in our previous video. So we are going to look at the input element using its name attribute, that is username. I'm going to copy this, so we can use this in our code.
Now, I'll head over to the Python script that we used in our previous video. So here, I am going to first change the name of the variable to username. And now I need a method to look at elements by name. So in Python Python Selenium, that method is simply called find element by name, pretty intuitive again. Here I will paste the username that I copied and I'm also going to change my print statements.
My input element is and I will also change this to the variable name. So with this, we have all the changes made in our file. Now let's go ahead and run this and see what the output is like. So here you see that the output printer is my input element is and the input element is printed after it's located by name, so in Python Selenium, this is how we look at HTML elements by name.
- Identify the steps for installing Python.
- Recognize the browsers supported by Selenium.
- Explain how to find an element when a unique ‘id’ is not available.
- Understand the purpose of the “send_keys.”
- Recall which exception is raised when a function tries to locate an element not yet loaded into the DOM.