How to check if an input text field exists in Selenium Python?

Selenium Python – Check if an input text field exists

In this tutorial, you will learn how to check if an input text field exists in Selenium Python.

To check if an input text field exists in Selenium Python, you can use try-except block around the statement to find the input text field element. If the element is not present, find_element() method throws NoSuchElementException. If the exception is not thrown, then we can know that the element exists, otherwise not.

The following is a simple code snippet to check if the input text field whose name attribute is 'dummy' exists in the webpage.

try:
    input_text_1 = driver.find_element(By.XPATH, "//input[@name='dummy']")
    print("Input text field exists.")
except NoSuchElementException:
    print("Input text field does not exist.")

Examples

In this example, we shall consider loading the webpage at URL: https://pythonexamples.org/tmp/selenium/index-18.html. The contents of this HTML file is given below. The webpage contains a couple of input text fields.

<html>
 <body>
  <h3>My Form</h3>
  <form action="">
    <label for="firstname">First name:</label>
    <input type="text" id="firstname" name="firstname"><br><br>
    <label for="lastname">Last name:</label>
    <input type="text" id="lastname" name="lastname"><br><br>
    <input type="submit" value="Submit">
  </form>
 </body>
</html>
Copy

1. Check if input text field with name=”firstname” exists

In the following program, we initialize a Chrome webdriver, navigate to specified URL, check if the input text field exists whose name attribute is 'firstname', and print the result to standard output.

Python Program

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(executable_path=ChromeDriverManager().install()))

# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-18.html')

# Check if value in input text is empty
try:
    input_text_1 = driver.find_element(By.XPATH, "//input[@name='firstname']")
    print("Input text field exists.")
except NoSuchElementException:
    print("Input text field does not exist.")

# Close the driver
driver.quit()
Copy

Output

Input text field exists.

Reference Tutorials

2. Check if input text field with name=”dummy” exists

In the following program, we initialize a Chrome webdriver, load the specified URL, check if the input text field exists whose name attribute is 'dummy', and print the result to standard output.

Python Program

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(executable_path=ChromeDriverManager().install()))

# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-18.html')

# Check if value in input text is empty
try:
    input_text_1 = driver.find_element(By.XPATH, "//input[@name='dummy']")
    print("Input text field exists.")
except NoSuchElementException:
    print("Input text field does not exist.")

# Close the driver
driver.quit()
Copy

Output

Input text field does not exist.

Summary

In this Python Selenium tutorial, we have given instructions on how to check if an input text field exists, with the help of examples.

Related Tutorials

Quiz on Selenium

Q1. What is a web driver in Selenium Python?

Not answered

Q2. Which of the following is not a popular web driver used in Selenium Python?

Not answered

Q3. What is the method used to switch to a different frame in Selenium Python?

Not answered

Q4. Which of the following is not a commonly used web element locator in Selenium Python?

Not answered

Q5. What is the method used to select a value from a drop-down list in Selenium Python?

Not answered
Code copied to clipboard successfully 👍