python logo

selenium input text python


Python hosting: Host, run, and code Python in the cloud!

Given a webpage containing a text area or text field, text can be automatically written using Python code.

Python can start a web browser, open the page and write code in it.
This is done with the the selenium framework.

Related course
Browser Automation with Python Selenium

Setup selenium

In this article we will demonstrate this automation using a small code snippet.

Start by importing the selenium module. This module is not installed by default, so if you don’t have it install it with the Python package manager (pip).

pip install selenium

The driver needs installed aswell as the web browser.

Selenium add textbox test

Lets take a look at the code. First you need the webdriver to create the web browser. You can open the target website using the method below


driver.get(url)

where the parameter specfies the website url.

We select the html element using the method find_element_by_id. The html element must have an id for this to work, otherwise you need xpath.


driver.find_element_by_id(id)

Then the browser writes text in the textbox using the method send_keys, as parameter it takes a string:


send_keys(str)

The complete code example:


from selenium import webdriver
import time

options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument("--test-type")
options.binary_location = "/usr/bin/chromium"
driver = webdriver.Chrome(chrome_options=options)
driver.get('http://codepad.org')

text_area = driver.find_element_by_id('textarea')
text_area.send_keys("This text is send using Python code.")

The browser will start automatically and starts adding the text to the textbox:

selenium textbox send_keys method writes in textbox

Download Selenium Examples

BackNext





Leave a Reply: