user2323844
user2323844

Reputation: 411

How to clear the previous value entered in text field and pass new value to same field in Selenium webdriver

i have written selenium script to read value from excel and pass it to text field. I am facing below mentioned issue

First i have passed invalid values to text field through sendkeys(). in second time, i am sending correct values to field through sendkeys(). But it is appending to the previous value.

How I can clear previously entered value in text field and pass new value to same fields

Can anyone provide solution for this

Upvotes: 0

Views: 3159

Answers (2)

stuartjay
stuartjay

Reputation: 47

This code should do the job in Java. Put it before you enter the second piece of text.

_driver.findElement(By.id/css/xpath("element")).clear();

Upvotes: 1

Robbie Wareham
Robbie Wareham

Reputation: 3438

element.clear
element.sendkeys(value)

if you want to make this a one line step which combines clear and sendkeys, then you have many options, some dependent on the language you are using.

eg.

  • create a helper method which takes element and value

  • extend Element class and create a new method called Setvalue

  • Overwrite the existing sendkeys with a new method which calls clear first

Each of these have pros and cons and may be dependent on the language you use

Upvotes: 1

Related Questions