所以我遇到了一个问题,我更改日期字段的命令只有一半时间有效,我不确定是什么原因造成的。然而,当我在 Debug模式下运行它们并逐步执行该方法时,它会执行它应该执行的操作。以下是我的方法中的代码:
PermitExpirationRangeStart = DateTime.Today.AddDays(121).ToShortDateString();
PermitExpirationRangeEnd = DateTime.Today.AddDays(150).ToShortDateString();
//Wait until we have the facility name.
//WaitFor(() => !string.IsNullOrEmpty(FindElement(By.Id("ctl32_ctl04_ctl05_txtValue")).GetAttribute("name")));
Sleep(5000);
FindElement(By.Id("ctl32_ctl04_ctl03_txtValue")).Clear();
FindElement(By.Id("ctl32_ctl04_ctl03_txtValue")).SendKeys(PermitExpirationRangeStart);
FindElement(By.Id("ctl32_ctl04_ctl05_txtValue")).Clear();
FindElement(By.Id("ctl32_ctl04_ctl05_txtValue")).SendKeys(PermitExpirationRangeEnd);
我要测试的有问题的页面是下面的代码(这只是日期字段之一):
<td class="ParamEntryCell" style="padding-right:0px;">
<div id="ctl32_ctl04_ctl05">
<div style="white-space:nowrap;" onactivate="event.cancelBubble=true;">
<input id="ctl32_ctl04_ctl05_txtValue" class="null" type="text" size="28" value="12/22/2016" name="ctl32$ctl04$ctl05$txtValue"/>
<input id="ctl32_ctl04_ctl05_ddDropDownButton" type="image" style="cursor:pointer;" title="Select a value" alt="Select a value" name="ctl32$ctl04$ctl05$ddDropDownButton" src="/Reports/Reserved.ReportViewerWebControl.axd?OpType=Resource&Version=12.0.4422.0&Name=Microsoft.Reporting.WebForms.calendar.gif"/>
</div>
</div>
</td>
我尝试重新安排测试中的代码行,我什至尝试在对它执行 Clear 或 SendKeys
方法之前将元素设置为 IWebElements
. Google 搜索没有找到有助于解决此问题的结果。
最佳答案
答案很可能是:需要在代码中引入wait。
在 Debug模式下,您一次“缓慢”地执行代码,浏览器实例有足够的时间来响应。
请尝试引入“等待”机制。
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("locator")));
对于您的 ctl32_ctl04_ctl03_txtValue
和 ctl32_ctl04_ctl05_txtValue
元素
关于C# Selenium - Click/Clear/SendKeys 不能正常工作,但在 Debug模式下工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38577899/