1

I have an input on a form I wish to test using Selenium. I have a form input of type number as below

<input required type="number" class="form-control" id="number" name="number" placeholder="Number" step="1" min="0" max="4">

I wish to test this form by using Selenium Chrome Driver. I have the following code

WebElement number = driver.findElement(By.id("number"));
number.sendKeys("2");

I have also tried

WebElement number = driver.findElement(By.id("number"));
int numberInt = 2;
number.sendKeys(Integer.toString(numberInt));

But get the following error and stack trace:

at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:285)
at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:106)

Any ideas how I can send a value of 2 to my input field?

peterbonar
  • 559
  • 3
  • 6
  • 24

1 Answers1

1

Your post does not give information on the exact exception you are facing. Without that we could only guess what the problem is. If you are using Java and Selenium, the Javascript code at the end is worth a try. One of the ways you can get a number(format) entered. Sendkeys only takes characters, there is no overloaded method that accepts numbers.

        driver.get("https://www.google.com");
        WebElement searchElement = driver.findElement(By.cssSelector("input[name='q']"));
        //standard sendkeys
        searchElement.sendKeys("123");
        searchElement.clear();

        //using actions class
        Actions action = new Actions(driver);
        action.moveToElement(searchElement).click().sendKeys("123").sendKeys("").build().perform();

        //use javascript, number is entered
        JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
        jsExecutor.executeScript("arguments[0].value = 456;", searchElement);
Vinay
  • 71
  • 1
  • 4