How to get the placeholder text using Selenium Python?

Most of the input fields in our system have a text prompt inside them called a “placeholder.” This placeholder provides instructions or examples of the type of information that should be entered into that particular field. It helps users understand what data is expected of them and ensures that the information entered is accurate and consistent.

As a tester, we may encounter scenarios where we need to verify the placeholder of an input field, and this post will cover various ways to get the placeholder text of an input field using Selenium and Python.

Let’s discuss both ways one by one

Using get_attribute() method

The get_attribute() method in Selenium using Python language allows us to retrieve the value of an attribute. In an input box, the value of the placeholder is stored in the “placeholder” attribute. Thus, by retrieving the value of the “placeholder” attribute, we can obtain the value of the placeholder text written inside the input box.

Syntax of using get_attribute method
element.get_attribute(attribute_name)
Code Example

Let’s take an example of one of the elements present at https://testkru.com/Elements/TextFields.

text filed with a placholder

Below is the DOM structure for the element.

DOM of a placeholder text field element

First, we can find the highlighted element with the help of the find_element() method using its “id” attribute.

driver.find_element(By.ID, 'lastNameWithPlaceholder')

Then, we will use the get_attribute() method to get the placeholder attribute’s value.

element.get_attribute("placeholder")

Whole code

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://testkru.com/Elements/TextFields")

element = driver.find_element(By.ID, 'lastNameWithPlaceholder')
attribute_value = element.get_attribute("placeholder")
print("Placeholder text: " + attribute_value)

Output –

Placeholder text: Enter your last name...
Using execute_script() method

The execute_script() method in Selenium Python helps in executing the javascript code, and thus, we can use it to write a script that can return us the value of the placeholder attribute.

driver.execute_script("return arguments[0].placeholder",element)

Whole code

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://testkru.com/Elements/TextFields")

element = driver.find_element(By.ID, 'lastNameWithPlaceholder')
attribute_value = driver.execute_script("return arguments[0].placeholder",element)
print("Placeholder text: " + attribute_value)

Output –

Placeholder text: Enter your last name...

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected].

Related Articles –

get_attribute() method in Selenium Python

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *