简体   繁体   中英

Selenium can not send keys at datetime textbox

I have a webapplication which is automated tested currently I have the following HTML content:

 <td> <input name="ctl00$ctl00$BaseRightContent$MainRightContent$FromTextBox" type="text" value="19.06.2019" id="ctl00_ctl00_BaseRightContent_MainRightContent_FromTextBox" class="wideUserInput"> <input type="hidden" name="ctl00$ctl00$BaseRightContent$MainRightContent$FromTextBoxMasked_ClientState" id="ctl00_ctl00_BaseRightContent_MainRightContent_FromTextBoxMasked_ClientState"> </td> 

For my selenium test I use this workaround:

 if (employmentParam.StartDate != null)
        {
            driver.FindElement(By.Id("ctl00_ctl00_BaseRightContent_MainRightContent_FromTextBox")).Clear();
            driver.FindElement(By.Id("ctl00_ctl00_BaseRightContent_MainRightContent_FromTextBox")).SendKeys(employmentParam.StartDate); //Here is my object (string) Example: employmentParam.StartDate --> 11.02.2100
        }

And currently I have the problem that my selenium inserts only '21.00.2002" instead of the given parameter '11.02.2100' This is how the webelement looks like:

在此输入图像描述

The desired element is a dynamic element so you have to induce WebDriverWait for the desired ElementToBeClickable() and you can use either of the following Locator Strategies as solutions:

  • CssSelector :

     var element = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("input.wideUserInput[id$='_BaseRightContent_MainRightContent_FromTextBox'][name$='FromTextBox']"))).Click(); element.Click(); element.Clear(); element.SendKeys(employmentParam.StartDate); 
  • XPath :

     var element = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[@class='wideUserInput' and contains(@id, '_BaseRightContent_MainRightContent_FromTextBox')][contains(@name, 'FromTextBox')]"))).Click(); element.Click(); element.Clear(); element.SendKeys(employmentParam.StartDate); 

Here is the solution for the problem:

               IWebElement wb = driver.FindElement(By.CssSelector("input.wideUserInput[id$='_BaseRightContent_MainRightContent_FromTextBox'][name$='FromTextBox']"));
                IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;
                jse.ExecuteScript($"arguments[0].value='{employmentParam.StartDate}';", wb);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM