C# - 如何在 Coded UI 中从 ExecuteScript 填充列表
C# - How to Populate a List from ExecuteScript in Coded UI
从这个 继续,其中 jQuery 片段在浏览器中有效,但是当我尝试将脚本的 return 值放入 C# 列表中时,它看起来很对象一片空白。我正在尝试从内部网页获取 listYears 中的年份,例如:2018、2017、2015、2012。 运行 jQuery 在浏览器上可以正常工作,因为它可以正确提取年份,但是当我尝试使用 C# 将年份 return 从 jQuery 编辑到列表中时,它看起来不是填充,或者我没有使用正确的 C# 构造。我在 C# 代码中犯了什么错误?
List<string> listYears = new List<string>();
listYears = (List<string>)window.ExecuteScript(@"$(document).ready(function(){
var scr = document.createElement('script');
scr.src = 'https://code.jquery.com/jquery-3.3.1.js';
document.body.appendChild(scr);
var years = []
var thisYear = 0;
$('time').each(function(){
thisYear = parseInt($(this).attr('datetime').split('-')[0]);
years.push(thisYear);
});
return years.toString();
});");
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("listYears: " + listYears[0]);
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("*************************");
或者,
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("listYears: " + listYears);
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("*************************");
经过反复试验,更正了问题,listYears 现在显示执行 jQuery 脚本后的所有年份。
var listYears = window.ExecuteScript(@"
$=jQuery;
var years = [];
var thisYear = 0;
$('time').each(function(){
thisYear = parseInt($(this).attr('datetime').split('-')[0]);
// do something with thisYear...
years.push(thisYear);
});
// Write all years to console window.
return years.toString();
");
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("listYears: " + listYears);
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("*************************");
从这个
List<string> listYears = new List<string>();
listYears = (List<string>)window.ExecuteScript(@"$(document).ready(function(){
var scr = document.createElement('script');
scr.src = 'https://code.jquery.com/jquery-3.3.1.js';
document.body.appendChild(scr);
var years = []
var thisYear = 0;
$('time').each(function(){
thisYear = parseInt($(this).attr('datetime').split('-')[0]);
years.push(thisYear);
});
return years.toString();
});");
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("listYears: " + listYears[0]);
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("*************************");
或者,
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("listYears: " + listYears);
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("*************************");
经过反复试验,更正了问题,listYears 现在显示执行 jQuery 脚本后的所有年份。
var listYears = window.ExecuteScript(@"
$=jQuery;
var years = [];
var thisYear = 0;
$('time').each(function(){
thisYear = parseInt($(this).attr('datetime').split('-')[0]);
// do something with thisYear...
years.push(thisYear);
});
// Write all years to console window.
return years.toString();
");
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("listYears: " + listYears);
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("*************************");
System.Diagnostics.Debug.WriteLine("*************************");