7

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");
  • 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? – Bharat Mane May 3 '16 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. – Rajat Sharma May 3 '16 at 11:38
  • What are you using to drive Selenium, and can you post your code? – Cronax May 3 '16 at 13:45
  • driver.findElement(By.name("phone")).sendKeys("(222)222-2222"); driver.findElement(By.id("ssn")).sendKeys("555-55-5555"); – Rajat Sharma May 5 '16 at 6:55
  • So is it working or not? what's your problem with this? What you expect from us? – Bharat Mane May 7 '16 at 6:02
11

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.

  • 1
    Second solution seems to have better chance of success – me_digvijay Mar 13 '18 at 7:20
  • The second solution worked well for me when even sending keys via a Robot object would not. – Doug Noel Aug 1 '18 at 13:59
  • The first solution worked for me thank you – wallop Jul 19 at 18:24
1

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
  • 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? – barlop Dec 28 '18 at 15:41
0

driver.findElement(By.name("Email")).sendKeys("automation@gmail.com");

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.

protected by Community Feb 21 '18 at 18:41

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.