14

I am not able to validate an international number text box in Selenium.

I have tried earlier by name, class and other locators but it is not inserting the value into the textbox.

Below is the text box and the corresponding HTML:

this the textbox

html content

The code I am using is:

driver.findElement(By.name("phone")).sendKeys("(222)222-2222");   
driver.findElement(By.id("ssn")).sendKeys("555-55-5555");
11
  • 1
    What exactly you want to do with these Text boxes? by using any locators you have to enter text in to the textbox or you have to validate these textboxes? Commented May 3, 2016 at 7:34
  • if you see in above image home phone has a format of international number ,now when i try to insert any number using any locator and using sendkeys method it is not accepting the input. Commented May 3, 2016 at 11:38
  • What are you using to drive Selenium, and can you post your code? Commented May 3, 2016 at 13:45
  • driver.findElement(By.name("phone")).sendKeys("(222)222-2222"); driver.findElement(By.id("ssn")).sendKeys("555-55-5555"); Commented May 5, 2016 at 6:55
  • So is it working or not? what's your problem with this? What you expect from us? Commented May 7, 2016 at 6:02

3 Answers 3

18

If sendkeys() methods are not working then use following two ways to input text:

  1. Before sendkeys() use click() method to click inside textfield i.e:

    driver.findElement(By.name("phone")).click();
    driver.findElement(By.name("phone")).sendKeys("(222)222-2222");   
    driver.findElement(By.id("ssn")).click();
    driver.findElement(By.id("ssn")).sendKeys("555-55-5555"); 
    
  2. Second way is to use javascript to input text in texfields like this:

    WebElement wb = driver.findElement(By.name("phone"));
    JavascriptExecutor jse = (JavascriptExecutor)driver;
    jse.executeScript("arguments[0].value='(222)222-2222';", wb);
    jse.executeScript("document.getElementById('ssn').value='555-55-5555';");
    

Please let me know if that works for you. Webdriver provided the javscript implementation library so that we could use javascript to perform actions on browser which are not possible with with webdriver native API. And in web automation testing services we encountered many such issues where we have to go beyond webdriver native API and have to use javscript methods.

3
  • 1
    Second solution seems to have better chance of success Commented Mar 13, 2018 at 7:20
  • 1
    The second solution worked well for me when even sending keys via a Robot object would not. Commented Aug 1, 2018 at 13:59
  • Unfortunately none of the solutions worked for me. From what I understand (as I am working with a credit card number input field) there is some sort of javascript validation that changes some property of the field right when the sendKeys is filling it up. As a result the string always looks incomplete. Commented Jun 20, 2020 at 23:07
2

First once clear the input text fields and send the input values. Create a reference object for better use:

webElement objPh=driver.findElement(By.name("phone"));   
webElement objSSN=driver.findElement(By.name("ssn"));

objPh.clear();   //To clear the phone text field    
objPh.sendkeys("(222)222-2222"); // To send the input values in phone text field

objSSN.clear(); //To clear the phone ssn text field 
objSSN.sendkeys("555-55-5555"); // To send the input values in ssn text field
1
  • why clear them? he said he can't input to them so whether he clears them or not wouldn't make a difference to whether or not he can input to them would it? And why do you think he might have been unable to sendkeys to the textboxes? Commented Dec 28, 2018 at 15:41
0

driver.findElement(By.name("Email")).sendKeys("[email protected]");

This should work to insert values/text into Text-box input field. If not working pls cross-check below points:

  1. Screen/Page is loaded completely
  2. Assert that textbox is enabled and visible

If you found any of above points failing, you should try:

WaitForElement(your_element_textbox)

This method simple implemented using implicit wait to give additional time to element/text/screen/page to get loaded.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.