How to Enter Text or Clear Text From a Text Box using the Selenium Module in Python



Python


In this article, we show how to enter or clear text from a text box using the selenium module in Python.

So the selenium module in Python is a very dynamic and useful module when working with web browsers such as Firefox or Chrome.

Using the selenium module, we can open up a browser, go to any URL we need to go to, and do things such as enter text into text boxes, such as a log in page or filling out a form.

For example, you can write custom Python code that can go to a website such as Twitter and log you into your account.

The example code we will do on this page opens up the Firefox web browser, goes to walmart's home page, and logs you in into your account.

In order to run the program on this page, you will need to install the selenium module, which you can do with the line, pip install selenium. You also will need to install either the geckodriver (if you are working with the Firefox web browser) or the ChromeDriver (if you are working with the Chrome web browser).



How to Enter Text Into a Text Box

So in order to select a text box in order to enter text into it, the first thing we have to do is find a way to identify the text box.

So when you go to walmart's home page, there are 2 text boxes: Phone, email, or username and Password.

Normally the best way to target an HTML element is through the id element.

So when we go to walmart's home page and select 'View Page Source', we find out that the id for the email or phone number text box is 'email'. The id for the password text box is 'password'.

Using this information, we can enter text into the text boxes and then submit any form such as user login.



The first thing we have to do before we can enter text into a text box is locate the desired text box.

The email text box has an id of 'email', so we use the line, browser.find_element_by_id('email'), to get this text box.

We then use the email.send_keys() function to enter text into this text box. In this case, it is 'abc@gmail.com'

We then do the same for the password text box.

The password text box has an id of 'password', so we use the line, browser.find_element_by_id('password'), to get this text box.

We then use the password.send_keys() function to enter text into this text box. In this case, it is '123'

You can then submit the form through the submit() function.



How To Clear Text From a Text Box

So let's say that you entered text into a text box using the send_keys() function, but you want to change that text to something else.

You can't simply use the send_keys() function again because this function appends to what is already in the text box. Instead, you need to clear the text field and then use the send_keys() function to add in the new text.

So to clear text from a text box, you simply use the identifier for the text box, in this case we have the variable, email, followed by the .clear() function.

This is shown in the code below.

So both text boxes have been cleared of any text entered into them, so now new text can be entered into them.

And this is how we can enter or clear text from a text box using the selenium module in Python.


Related Resources

How to Create a Zip File in Python

How to Extract All Files and Folders from a Zip File in Python

How to Read the Contents of a Zip File in Python



HTML Comment Box is loading comments...