40

When I enter text into the text field it gets removed.

Here is the code:

String barcode="0000000047166";

WebElement element_enter = _driver.findElement(By.xpath("//*[@id='div-barcode']"));

element_enter.findElement(By.xpath("//html/body/div[1]/div[3]/div[1]/form/div/div/input")).sendKeys("barcode");
Unihedron
  • 10,902
  • 13
  • 62
  • 72
Adnan Ghaffar
  • 1,345
  • 7
  • 26
  • 46

4 Answers4

41

Agree with Subir Kumar Sao and Faiz.

element_enter.findElement(By.xpath("//html/body/div[1]/div[3]/div[1]/form/div/div/input")).sendKeys(barcode);
Nakilon
  • 34,866
  • 14
  • 107
  • 142
Eshan Liyanagama
  • 576
  • 1
  • 7
  • 11
  • i am doing by mentioned below code: element_enter.findElement(By.xpath("//html/body/div[1]/div[3]/div[1]/form/div/div/input")).sendKeys(barcode); but problem is that text gets removed automatically from the text field after entering. – Adnan Ghaffar Oct 03 '13 at 04:58
  • what do u mean by "text gets removed automatically from the text field after entering" ? – Eshan Liyanagama Oct 03 '13 at 05:12
  • text doesn't display in the text field by using mentioned above code. (text is enters into the text field and removes withing 3 to 5 seconds.) – Adnan Ghaffar Oct 03 '13 at 05:16
9

I had a case where I was entering text into a field after which the text would be removed automatically. Turned out it was due to some site functionality where you had to press the enter key after entering the text into the field. So, after sending your barcode text with sendKeys method, send 'enter' directly after it. Note that you will have to import the selenium Keys class. See my code below.

import org.openqa.selenium.Keys;

String barcode="0000000047166";
WebElement element_enter = driver.findElement(By.xpath("//*[@id='div-barcode']"));
element_enter.findElement(By.xpath("your xpath")).sendKeys(barcode);

element_enter.sendKeys(Keys.RETURN); // this will result in the return key being pressed upon the text field

I hope it helps..

CODEBLACK
  • 1,239
  • 1
  • 15
  • 26
3

Use this code.

driver.FindElement(By.XPath(".//[@id='header']/div/div[3]/div/form/input[1]")).SendKeys("25025");
Nakilon
  • 34,866
  • 14
  • 107
  • 142
1

It might be the JavaScript check for some valid condition.
Two things you can perform a/c to your requirements:

  1. either check for the valid string-input in the text-box.
  2. or set a loop against that text box to enter the value until you post the form/request.
String barcode="0000000047166";

WebElement strLocator = driver.findElement(By.xpath("//*[@id='div-barcode']"));
strLocator.sendKeys(barcode);
quamrana
  • 37,849
  • 12
  • 53
  • 71
Kumar
  • 329
  • 2
  • 6