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?

4
  • 1
    can you please provide more details like url and your code including browser and webdriver details? Commented Apr 14, 2020 at 16:04
  • Check stackoverflow.com/questions/60816270/… Commented Apr 14, 2020 at 16:52
  • 1
    Is that the complete stacktrace? Commented Apr 14, 2020 at 17:03
  • 1
    What is the exception message as well? Commented Apr 14, 2020 at 19:29

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.