Automated testing Python Selenium WebDriver

How to use now all the WebElements you found

You found elements on the page that you need to test. Now you need to click one element and check the text of the other. Or maybe you need to check whether the element enabled or clickable.

If you don’t know how to find elements of the page read my post about that: Locate web elements on the sites easily.

Are you just getting started with Selenium? Check out my post about initial Selenium configuration.

So let’s look at all the possible actions we can perform with an element found by Selenium WebDriver.

Get the element’s text

When testing web applications, it is often necessary to check that the text of an element is as expected. To do this, you will use one of these approaches.

The first approach is to get the “innerText” attribute from the element

element = driver.find_element(By.ID, "some-id")

And the second one is a separate method that gets the element’s text

element = driver.find_element(By.ID, "some-id")

In both cases listed above you will get a text of the element in string format.

Get the value of the element

The value of the element is something that is specified inside the input element. For example a text in an input field or an item that is selected in a drop-down.

element = driver.find_element(By.ID, "some-id")

Get an element’s style property

Let’s say you need to make sure that the font size or color of the text is correct. These options are usually set using style settings in a CSS properties. In order to get these parameters, the “value_of_css_property” method is used. This is how we can get the font size of the desired element:

element = driver.find_element(By.ID, "some-id")

Find out the name of an element’s tag

Sometimes, in order to make sure that an element is displayed correctly, you need to know the name of its tag. The “tag_name” method will help with this.

element = driver.find_element(By.ID, "some-id")

Click the element

Clicking is one of the most common actions you will perform on an element. This action is as simple as clicking a mouse button. Just find the element and click it. For example, you’ve found a link and now you click it.

element = driver.find_element(By.LINK_TEXT, "Follow Us")

Entering text in an input field

When you need to test a search or a login form, you need to enter text in various fields. This is done using the “sendKeys” method. For example, you fill the login field with the value “admin”:

element = driver.find_element(By.ID, "some-id")

Additionally, use the same method if you need to upload a file using the standard upload field. Similarly to entering a text you can send keys with a path to the file.

Standard upload field

Clearing the input field

In addition to the method described above, you need to know one more thing. If there is already some text in the input field, but you don’t need it, you can delete it with a single command.

element = driver.find_element(By.ID, "some-id")

Form submission

Finally, when you’re dealing with a form, whether it’s a login or a checkout, you can avoid clicking a button to submit the form. Just apply the “submit” method to any field of this form.

element = driver.find_element(By.ID, "some-id")

Checking the state of an element

As part of a feature test, you may want to know the current state of an element before or after you perform an action on it. Here is a list of statuses you can check:

  • selected
  • displayed
  • enabled
element = driver.find_element(By.ID, "some-id")

Each method returns a boolean value of the requested state.

Of course, for complete knowledge, be sure to read the official documentation on the selenium website

By Eugene Okulik