C# Selenium - Click/Clear/SendKeys 不能正常工作,但在 Debug模式下工作

标签 c# visual-studio selenium

所以我遇到了一个问题,我更改日期字段的命令只有一半时间有效,我不确定是什么原因造成的。然而,当我在 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_txtValuectl32_ctl04_ctl05_txtValue 元素

关于C# Selenium - Click/Clear/SendKeys 不能正常工作,但在 Debug模式下工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38577899/

相关文章:

c# - 如何声明一个 If 来验证一个类是否具有特定属性?

c# - Web API - REST 帮助和说明

c# - 基本对象 WebView 如何在 HTML 树中导航?

visual-studio - Visual Studio 包管理器控制台的键盘快捷键?

visual-studio - VS 2015 测试资源管理器为空

java - Selenium Webdriver click()没有做任何事情

c# - 非多行左对齐选项卡控件

c# - 查看类/子类对象

c++ - 我可以在 Visual Studio 2017 调试器中查看未处理的异常吗?

html - 具有特定值的任何属性的所有元素的 XPath?