Reputation: 313
I've two input fields in the form and their source is mentioned below
<input class="col8 last right i-f ssn" data-val="true" data-val-regex="Please enter valid SSN number" data-val-regex-pattern="^((\d{3}-\d{2}-\d{4}|X{3}-X{2}-X{4}))$" id="SSN" name="SSN" type="text" value="" />
<input class="col8 last right i-f" id="MiddleName" maxlength="15" name="MiddleName" onkeypress="return isAlphabetKey(event)" type="text" value="" />
I am trying to send input using the following command in selenium web driver (Firefox)
driver.findElement(By.cssSelector("input[id='SSN']")).sendKeys("55555");
driver.findElement(By.cssSelector("input[id='MiddleName']")).sendKeys("xyz");
for the first field no error is appearing while I run this under TestNG but second field is working fine I've included the page source for both input fields for ease.
Upvotes: 1
Views: 9071
Reputation: 86
Just remove the attribute that formats the input tag. Then you can just use SendKeys().
Upvotes: 1
Reputation: 313
If there is any implementation of JQuery on a text field sendKey() for selenium will not work you have to use the following code snippet
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("document.getElementById('field_id').value = 'field_val';");
Upvotes: 2
Reputation: 402
Possible Solution 1: Execute javascript call to sendKeys() As you stated, it is most likely the formatting of the textbox that is not allowing you to sendKeys() to the input box. The reason might just be the way that WebDriver handles putting input into a box. I'd recommend trying javascript to execute a different type of input filling method.
WebDriver driver = new FirefoxDriver();
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("document.getElementById("SSN").value='55555';);
Possible Solution 2: https://github.com/dsheiko/autofill
I don't have a good code snippet that i can show you for utilizing autofill, but the github itself has pretty nice examples.
Possible Solution 3: Remove the formatting attribute of the input tag, then use sendKeys()
((JavascriptExecutor) driver).executeScript("document.getElementsByID('SSN'[0].removeAttribute('data-val-regex-pattern');");
WebElement input= driver.findElement(By.id("SSN"));
input.clear();
input.sendKeys("55555");
Upvotes: 6