编码的UI – C# – time itemprop标签 – 如何validation年份存在

我昨天在一个稍微不同的问题上得到了@dlatikay的帮助,这帮助我部分解决了误报。 这是代码片段:

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),则count返回1,否则count返回0是否可以将所有年份放在列表/数组中,我可以参考使用C#代码? 我将感谢任何能够指导我正确方向的示例代码。

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

 March 8, 2018 December 7, 2017 November 23, 2017 October 13, 2017 September 16, 2017 August 25, 2017 August 17, 2017 July 25, 2017 May 23, 2017 May 12, 2017 

这应该让你开始。 它会将年份添加到数组中:

 $(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); });