我收到了来自 @dlatikay 的帮助略有不同question昨天,这帮助我部分解决了误报错误。这是代码片段:
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# 代码进行引用?我将感谢任何能够引导我走向正确方向的示例代码。
这是我正在测试的页面的片段。
<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);
});
关于javascript - 编码 UI - C# - 时间 itemprop 标签 - 如何验证年份是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50765069/