Huma
Huma

Reputation: 71

How to delete the default value in a text field and type a new value using Selenium WebDriver

In my application i have to type some value in the text field. This text field contain some default value therefore first i need to delete the value and then type the new value, But neither am able to delete this value nor type a new type a new value. Any help would be much appreciated. I have tried following code:

WebElement samount=d1.findElement(By.id("ctl00_ContentPlaceHolder1_txtAmt"));
System.out.println(samount);
samount.clear();
samount.sendKeys("100");

I also tried this:

WebElement searchField=d1.findElement(By.id("ctl00_ContentPlaceHolder1_txtAmt"));
System.out.println(searchField);
searchField.click();
searchField.clear();
searchField.sendKeys(Keys.BACK_SPACE );
searchField.sendKeys(Keys.chord(Keys.CONTROL, "a"));
searchField.sendKeys(Keys.DELETE);
searchField.sendKeys("100");
searchField.click();

The following is the HTML of my page:

<table cellspacing="6" cellpadding="6" style="width: 100%;">
<tbody>
<tr>
<tr>
<td style="text-align: center;" colspan="2">
<br>
<table width="100%" style="text-align: center;">
<tbody>
<tr>
<tr>
<td id="ctl00_ContentPlaceHolder1_tdFirst" style="width:35%;"> </td>
<td style="text-align: right;">
<td style="text-align: left;">
<input id="ctl00_ContentPlaceHolder1_txtAmt" class="txtAlpha" type="text"        style="width:100px;"       onblur="checkSender();" onkeypress="if    (WebForm_TextBoxKeyHandler(event) == false) return        false;AmountOnly();"    onchange="javascript:setTimeout('__doPostBack       (\'ctl00$ContentPlaceHolder1$txtSenderAmt\',\'       \')', 0)" maxlength="10" value="0.00"      name="ctl00$ContentPlaceHolder1$txtSenderAmt">

Upvotes: 0

Views: 2571

Answers (2)

Satish
Satish

Reputation: 752

WebDriverWait wait = new WebDriverWait(driver, 18);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ctl00_ContentPlaceHolder1_txtAmt")));
driver.findElement(By.id("ctl00_ContentPlaceHolder1_txtAmt")).clear();
driver.findElement(By.id("ctl00_ContentPlaceHolder1_txtAmt")).sendKeys("Enter-New-Text");

Upvotes: 1

Emma Burrows
Emma Burrows

Reputation: 5144

Your code looks okay - I recently wrote a Selenium test driver for an ASP.Net app and I have practically the same code as your first three lines (right down to the "ctl00_ContentPlaceHolder1_..." ;). But this means that to help you, we'll probably need more of your code -- like how "d1" is created -- because that might be where you actually have a problem.

Having said that, the first thing I notice looking at my own code is that I had to insert a wait in the following form just because trying to interact with the web page (this is all one statement, and ffDriver is my FirefoxDriver object):

(new WebDriverWait(ffDriver, 100))
  .until(ExpectedConditions.presenceOfElementLocated(By.id("main-content")));

So my first instinct is that Selenium might be trying to find the element before the web page has loaded (though IIRC, you'd usually get a clearer error if so). Based on your updated question, it's possible that the presence of that JavaScript attached to the element might be slowing things down from Selenium's point of view.

Upvotes: 1

Related Questions