【问题标题】:Using Selenium to automatically select a date in a datepicker使用 Selenium 在日期选择器中自动选择日期
【发布时间】:2012-04-29 08:40:19
【问题描述】:

我在尝试自动化测试以从日期选择器中选择日期时遇到问题(没有日期输入字段)。我已经搜索并看到了几个示例,其中一个特别说明您可以将它与 By.CssSelector 一起使用,但我不确定在此具体需要指定什么。

这是 HTML:

<td class="dpTD" onmouseout="this.className=&quot;dpTD&quot;;" 
onmouseover="this.className=&quot;dpTDHover&quot;;" 
onclick="updateDateField('03/04/2012', '2', '3', '3', '2012');">3</td>

这是 C#:

WebDriver.FindElement(By.CssSelector("inputtexthere")).Click();

【问题讨论】:

  • 我们可以看到更多的标记,例如包含此 td 的表?

标签: c# selenium css-selectors


【解决方案1】:

看起来你可以通过它的类来选择它:

WebDriver.FindElement(By.CssSelector(".dpTD")).Click();

或者,如果该类有多个单元格,请尝试使用 :contains() 缩小范围:

WebDriver.FindElement(By.CssSelector(".dpTD:contains('3')")).Click();

甚至是属性子字符串选择器:

WebDriver.FindElement(By.CssSelector(".dpTD[onclick*=updateDateField]")).Click();

我只是在这里提出建议,因为没有足够的标记可供使用。如果您使用过 CSS,则几乎可以将任何选择器与 Selenium 一起使用。

【讨论】:

  • 谢谢!使用属性子字符串选择器解决了我的问题。
猜你喜欢
  • 2017-05-01
  • 1970-01-01
  • 2014-02-19
  • 2021-07-28
  • 1970-01-01
  • 1970-01-01
  • 2019-03-22
  • 2018-01-17
  • 1970-01-01
相关资源
最近更新 更多