编码 UI - C# - 时间 itemprop 标签 - 如何验证年份是否存在

Coded UI - C# - time itemprop tag - how to validate year exists

我昨天收到了 @dlatikay in a slightly different 的帮助,这帮助我部分解决了误报错误。这是代码片段:

string countCurrentYearQA = (string)(window.ExecuteScript("count = 0; if(document.body.innerText.toString().indexOf('" + nowYearQAString + "')  > -1){count = 1;} return count.toString();"));
string countPastYearQA = (string)(window.ExecuteScript("count = 0; if(document.body.innerText.toString().indexOf('" + pastYearQAString + "')  > -1){count = 1;} return count.toString();"));

string countCurrentYearQAWithoutEndPipe = (string)(window.ExecuteScript("count = 0; if(document.body.innerText.toString().indexOf('" + nowYearQAStringWithoutPipe + "')  > -1){count = 1;} return count.toString();"));
string countPastYearQAWithoutEndPipe = (string)(window.ExecuteScript("count = 0; if(document.body.innerText.toString().indexOf('" + pastYearQAStringWithoutPipe + "')  > -1){count = 1;} return count.toString();"));

发生的情况是,在大多数情况下,JavaScript 能够检查是否存在任何特定年份。但是页面上的任何地方可能都提到了年份,这可能会导致误报。

我正在寻找一个示例,我可以在其中执行 JavaScript 来定位时间标签,因为它具有带有 itemprop="datePublished" 的日期时间值。我只需要检查年份是否匹配。获取所有 itemprop 值的方法是什么,只提取年份(4 位数字),如果年份与我在执行脚本中使用的年份字符串匹配(例如:nowYearQAString),则计数 returns 1,否则计数 returns 0。是否可以将所有年份都放在 list/array 中,我可以使用 C# 代码参考?如果有任何示例代码能指引我走向正确的方向,我将不胜感激。

这是我正在测试的网页的片段。

<time itemprop="datePublished" datetime="2018-03-08T11:00:00">March 8, 2018</time>

<time itemprop="datePublished" datetime="2017-12-07T12:40:00">December 7, 2017</time>

<time itemprop="datePublished" datetime="2017-11-23T10:00:00">November 23, 
2017</time>

<time itemprop="datePublished" datetime="2017-10-13T09:00:00">October 13, 2017</time>

<time itemprop="datePublished" datetime="2017-09-16T00:00:00">September 16, 2017</time>

<time itemprop="datePublished" datetime="2017-08-25T00:00:00">August 25, 2017</time>

<time itemprop="datePublished" datetime="2017-08-17T11:00:00">August 17, 2017</time>

<time itemprop="datePublished" datetime="2017-07-25T00:00:00">July 25, 2017</time>

<time itemprop="datePublished" datetime="2017-05-23T00:00:00">May 23, 2017</time>

<time itemprop="datePublished" datetime="2017-05-12T15:00:00">May 12, 2017</time>

这应该可以帮助您入门。它将年份添加到数组中:

$(document).ready(function(){
  var years = [];
    $('time').each(function(){
     var thisYear = $(this).attr('datetime').split('-')[0];
     // do something with thisYear...
     years.push(thisYear);
  });
  // Write all years to console window.
  console.log(years);
});