0

I'm trying to end a text to the input field named City (HTML code is mentioned below) using

driver.findElement(By.id("City")).sendKeys("Brussels");

The script runs and inserts the city as Brussels for few seconds and then the value 'Brussels' vanishes immediately. The script then errors out with the message

Exception Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: element not interactable

HTML Code*<input class="form-control input-block-level form-text required" id="City" autocomplete="new-password" data-validator="validateFieldText" data-errorfield="cityError" onblur="validate(this)" onfocus="focusField(this)" data-drupal-selector="edit-city" type="text" name="City" value="" size="60" maxlength="128" placeholder="City" required="required" aria-required="true">

Ryan Wilson
  • 10,223
  • 2
  • 21
  • 40
Bimlesh
  • 269
  • 2
  • 9
  • 20
  • 1
    I would read the accepted answer on this related post, it details what this exception can be generated by, as well as other solutions to get around the exception using the `webdriver` - (https://stackoverflow.com/questions/49864965/org-openqa-selenium-elementnotinteractableexception-element-is-not-reachable-by), I personally have found using the `JavascriptExecutor` to be my favorite way to circumvent `selenium` exceptions and set values to hidden elements or elements which were out of view. – Ryan Wilson Sep 03 '20 at 20:46

2 Answers2

1

Basically from what Ryan said you can use either of the following two solutions depending on what the issue is. Due to your element not being interactable.

Webdriver wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.id("City"))).sendKeys("Brussels");

Or using Javascript executer.

String inputText = "Brussels";
WebElement element = driver.findElement(By.id("City"));
String js = "arguments[0].setAttribute('value','"+inputText+"')"
((JavascriptExecutor) driver).executeScript(js, myElement);
Arundeep Chohan
  • 9,779
  • 5
  • 15
  • 32
0

Wait for the page to get loaded fully. once the element is loaded correctly, may be then try to enter text into the text page.

Hitesh
  • 31
  • 7