0

I am trying to enter the text and ensure it gets saved. In my project, there is no save button, as soon as it entered it gets saved. Only few fields that cracks me when entering the text.

Note: I'm using the latest version of Chrome and passing the numeric values as text.

I referred to below pages and updated my code accordingly, then also this issue persists:

Below is the code that I used,

 public void EnterValuesByIndex(String locator, String locatorValue, String text, int indexvalue) throws InterruptedException
      {
        WebElement element = null;

        if (locator.equalsIgnoreCase("cssSelector")) {
          element = (WebElement)driver.findElements(By.cssSelector(locatorValue)).get(indexvalue - 1);

        } else if (locator.equalsIgnoreCase("xpath")) {
          element = (WebElement)driver.findElements(By.xpath(locatorValue)).get(indexvalue - 1);

        } else if (locator.equalsIgnoreCase("id")) {
          element = (WebElement)driver.findElements(By.id(locatorValue)).get(indexvalue - 1);
        }
        //element.clear();
        element.sendKeys(text);
        Thread.sleep(2000);
        element.sendKeys(Keys.ENTER);
        System.out.println("Enter key is pressed");
      }

Below code also used to enter character by character, same issue persists,

public void EnterTextbyChar(String locator, String locatorValue, String text, int indexvalue) throws InterruptedException
        {
            String value = text;                
            WebElement element = null;

            if (locator.equalsIgnoreCase("cssSelector")) {
            element = (WebElement)driver.findElements(By.cssSelector(locatorValue)).get(indexvalue - 1);
            } else if (locator.equalsIgnoreCase("xpath")) {
                element = (WebElement)driver.findElements(By.xpath(locatorValue)).get(indexvalue - 1);

            } else if (locator.equalsIgnoreCase("id")){
              element = (WebElement)driver.findElements(By.id(locatorValue)).get(indexvalue - 1);               }
            element.clear();
            for (int i = 0; i < value.length(); i++)
            {
              char c = value.charAt(i);
              String s = new StringBuilder().append(c).toString();
              element.sendKeys(s);
              element.sendKeys(Keys.RETURN);
              element.click();
              Thread.sleep(2000);
              System.out.println("Return key is pressed in EnterTextByChar method");
              System.out.println(c);
             }  
         }
Roja
  • 293
  • 1
  • 5
  • 21
  • can you specify actual error you are getting/ problem you are facing? – vsbehere Jun 28 '17 at 11:35
  • There is no error come out. The typed text just vanished immediately after tabbing to next field. Due to which the mandatory fields are left blank and that made me not to proceed further. – Roja Jun 29 '17 at 04:52

2 Answers2

1

Finally a solution found. After entering the text, sending the upwards key that worked perfectly.

    element.sendKeys(text);
    element.sendKeys(Keys.UP);
Roja
  • 293
  • 1
  • 5
  • 21
1

If other options don't work, try element.sendKeys(text); element.sendKeys(Keys.RETURN);

Ravi
  • 11
  • 2