0

I know this might be a repeated question but I tried various methods to solve this one but couldn't solve the situation

enter image description here enter image description here

HTML code copied from the website

<input name="ctl00$TabContainer1$tpMain$ContentPlaceHolder1$Ins$txtDob" type="text" value="(dd/mm/yyyy)" maxlength="10" id="ctl00_TabContainer1_tpMain_ContentPlaceHolder1_Ins_txtDob" onselect="javascript:if(this.value=='(dd/mm/yyyy)') this.value='';this.style.color='';" onblur="javascript:InputValidation(this,'Date','Date of Birth','(dd/mm/yyyy)');" onclick="javascript:if(this.value=='(dd/mm/yyyy)') this.value='';this.style.color='';" onkeydown="return autodate(this)" onchange="javascript:CheckChanges();" onkeypress="return onlyNumbersWithForwardslash();" style="width:90%;" tabindex="0">

My code goes like this

string dob = startDate.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
string[] array = dob.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
char[] Chararray = string.Join(string.Empty, array).ToCharArray();

IWebElement DobElement = driver.FindElement(By.XPath("//*[@id='ctl00_TabContainer1_tpMain_ContentPlaceHolder1_Ins_txtDob']")); // .Id("ctl00_TabContainer1_tpMain_ContentPlaceHolder1_Ins_txtDob"));
    DobElement.Clear();

    foreach (char s in Chararray)
    {
         int num = Int32.Parse(s.ToString());
         DobElement.SendKeys(""+num);
    }

anyone help is appreciated to input numerical numbers to the textbox

4
  • What is your question here? anyone help in what? Where are you stuck? Error stack trace? Commented Mar 28, 2019 at 6:45
  • @DebanjanB there is no error throwing up numbers are not being inputted in to the textbox Commented Mar 28, 2019 at 6:48
  • You need to update the question with text based HTML instead of a picture. Commented Mar 28, 2019 at 6:50
  • @DebanjanB copied the html code also here Commented Mar 28, 2019 at 6:54

1 Answer 1

2

As the desired element is a JavaScript enabled element, so to send a chracter sequence within the <input> field you need to induce WebDriverWait for the desired element to be clickable and you can use either of the following solutions:

  • CssSelector:

    IWebElement DobElement = driver.FindElement(By.CssSelector("input[id$='_Ins_txtDob'][name$='txtDob'][onblur*='InputValidation')]"));
    DobElement.Click();
    DobElement.Clear();
    DobElement.SendKeys(""+num);
    
  • XPath:

    IWebElement DobElement = driver.FindElement(By.XPath("//input[contains(@id, '_Ins_txtDob') and contains(@name, 'txtDob')][contains(@onblur, 'InputValidation')]"));
    DobElement.Click();
    DobElement.Clear();
    DobElement.SendKeys(""+num);
    
Sign up to request clarification or add additional context in comments.

2 Comments

@DebenjanB could you explain the code why to include the onBlur but not other events like keypress, keydown
As you pointed out, virtually we could have considered onkeydown or onkeypress just like onblur. But if you observe the onkeydown event it's pretty generic and can be present for majority of the <input> elements. Same goes for onkeypress. Both of the alternatives should work. I choose to pick onblur as this event will be the last event among the three events to be fired which may be the last event to be rendered within the HTML.

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.