Part 12: Usages Of Javascripts In Selenium : How To Type In Input Box Using Javascript

Hello Folks,

It is asked in interviews sometimes about alternative of sendKeys() method in selenium webdriver. We will see the same thing here.

Javascript is the alternative for sendKeys method. First basic question why we would require alternative of sendKeys? You must have encountered some text box which is nested and selenium is not able to interact. Another place I find is selecting a date from calendar. Different applications use different types of calendar and it is difficult to create methods for each type of calendar. Javascript saves you here.

HTML web element has an attribute called “value” which stores the current value. If we type “Amod” in a First Name text box, “value” attribute of text box will be as “value=Amod”. Using javascript, we can directly set the value of “value” attribute.

Java Code:



If you want to learn how to select a date using Javascript, read below post.

How to select a date in calendar using Javascript.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.


Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning