简体   繁体   中英

Selenium WebDriver C#, Unable to Enter value in a textbox

How do i identify the text box and place a value in the text box ?

Code Snippet

<div id="CTR_StartDate" class=" datePickerXtreme datePickerContainer datePickerXtreme datePickerContainer datePickerXtreme datePickerContainer">
    <input tabindex="0" name="ct100$CTR_StartDatectl00" value="Date" id="CTR_StartDate_ctl00" class="datePickerXtremeDateOnly   validField" type="text">
    <a tabindex="0" href="#" class="ui-datepicker-icon">&nbsp;</a>
    <input tabindex="0" value="Tuesday, 10 January 2017 00:00:00" name="ct100$CTR_StartDatectl01" id="CTR_StartDate_ctl01" type="hidden">
    <input tabindex="0" value="Tuesday, 10 January 2017 00:00:00" name="ct100$CTR_StartDatectl02" id="CTR_StartDate_ctl02" type="hidden">
    <span id="CTR_StartDate_ctl04" category="Common" style="color:Red;visibility:hidden;"></span>
    <input tabindex="0" name="ct100$CTR_StartDatectl03" id="CTR_StartDate_ctl03" value="GMT Standard Time" type="hidden">
</div>

Below is the code that i am using to place the text:

IWebElement ContractStartDate = driver.FindElement(By.Id("CTR_StartDate_ctl00"));
wait.Until(ExpectedConditions.ElementIsVisible(By.Id("CTR_StartDate_ctl00")));
ContractStartDate.SendKeys("14/07/2016");        

I have tried it with Name, it still doesn't work. Few times it does work. Thats very rare though. I have tried clicking on the text box and placing the value using send keys, still doesn't work. Sometimes strange exceptions occur.

Can someone help me with this please ?

can you try and use a Css selector?

String selector = "div.datePickerXtreme input.datePickerXtremeDateOnly";
IWebElement ContractStartDate = driver.FindElement(By.CssSelector(selector));
ContractStartDate.Clear();
ContractStartDate.SendKeys("14/07/2016");

May be your problem is that you are going to find element first then check it's visibility and then use .sendKeys() on already found element which may be visible or not stored alraedy into ContractStartDate that's why your test case sometime pass and some time fail. Try as below :-

IWebElement ContractStartDate = wait.Until(ExpectedConditions.ElementIsVisible(By.Id("CTR_StartDate_ctl00")));
ContractStartDate.Click();
ContractStartDate.SendKeys("14/07/2016");

Hope it will work...:)

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