Dhinesh
Dhinesh

Reputation: 139

Unable to enter value in text box using selenium

I am trying to automate updating my number in Naukri.com. I was able to get the value from the text box, but unable to update the value. This is what I tried:

WebElement mobileNo = driver.findElement(By.id("mobile")); 
if(mobileNo.getAttribute("value").equals("9912345678"))
{
    System.out.println("Test Ran");
    mobileNo.clear();
    mobileNo.sendKeys("+91 9912345678");
}
else if(mobileNo.getAttribute("value").equals("+91 1234567891"))
{
    mobileNo.clear();
    mobileNo.sendKeys("9912345678");
}
System.out.println("Test Ran");

Upvotes: 1

Views: 2122

Answers (1)

Saifur
Saifur

Reputation: 16201

The reason being the phone number is set to the value attribute and only way that I know to update that is to use JavaScript with Selenium. You can easily use the following JavaScript and JQuery to simply update the phone number.

WebElement mobileNo = driver.findElement(By.id("mobile")); 
JavascriptExecutor jscript = (JavascriptExecutor)driver;

if(mobileNo.getAttribute("value").equals("9912345678"))
{
    System.out.println("Test Ran");
    jscript.executeScript("$('#mobile').attr('value','+91 9912345678')");

}
else if(mobileNo.getAttribute("value").equals("+91 9944991706"))
{
    jscript.executeScript("$('#mobile').attr('value','9912345678')");
}
System.out.println("Test Ran");

Upvotes: 1

Related Questions