How to insert Text in Input tag using Javascript Executor.

1,910 views
Skip to first unread message

sachin ahuja

unread,
Jan 9, 2015, 8:51:09 AM1/9/15
to seleniu...@googlegroups.com
Hi Friends,

I am stuck in a situation and not able to insert value in a text box.
Below is the code for the same:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('value', arguments[1]);",element,value1);

where
  • driver is WebDriver object
  • element is WebElement object
  • value1 is the text
same code is working for other web pages.

WebElement is as:

<input class="ng-dirty ng-valid ng-valid-required" type="text" pattern="job_title" required="" data-ng-model="jobTitle"></input>

and when I execute above code it didn't show me any thing in text field but change the tag as 

<input class="ng-dirty ng-invalid ng-invalid-required" type="text" pattern="job_title" required="" data-ng-model="jobTitle" data-invalid="" value="Title"></input>


Please help me in resolving the same.

Thanks,

PeterJeffreyGale

unread,
Jan 9, 2015, 9:22:24 AM1/9/15
to seleniu...@googlegroups.com
This is a Selenium forum ... So why not use the Web driver "input" funtionality provided?

Selenium Framework

unread,
Jan 10, 2015, 4:40:21 PM1/10/15
to seleniu...@googlegroups.com
Yes this is a Selenium forum and you are asking javascript. You might want to familiarize yourself with Javascript basics on w3schools for further questions on this.
For your specific question, since it was on the top of my head, see the below link. As mentioned, please post ONLY selenium questions in this forum in future


Cheers,

David

unread,
Jan 11, 2015, 7:38:12 PM1/11/15
to seleniu...@googlegroups.com
Be warned that setting or changing values this way via javascript may not auto update the visual rendering of the element on the page, so it looks the same visually but in the DOM or HTML source the value is set correctly. Therefore, if you then submit the value in a form post or some AJAX call, etc. taking you to another page or not, the expected value should be ok/correct. But if you're just validing things in the UI on same page, that may not work.

Better using native Selenium methods as others suggested. Doing it in javascript against the mentioned specific input element, you might need to trigger something else (some other javascript methods after setting value or before) to trigger the update visually.
Reply all
Reply to author
Forward
0 new messages