javascript - 如何在 c# 中处理来自 javascript executor(selenium) 的输出
问题描述
我有下面的代码行来查找给定字符串的所有匹配元素
var coll = ((IJavaScriptExecutor)driver).ExecuteScript(string.Format(" var x = $(arguments[0]).find(\":contains('{0}')\"); return x;", strVerify), element);
当它找到匹配时,它返回 IWebElement 的 ReadOnlyCollection,否则它返回对象的 ReadOnlyCollection。
我正在尝试编写一种通用方法来处理上述输出,但我做不到。
meth1 在返回 IWebElement 的 ReadOnlyCollection 时给出以下错误
无法将类型为“System.Collections.ObjectModel.ReadOnlyCollection`1[OpenQA.Selenium.IWebElement]”的对象转换为类型“System.Collections.ObjectModel.ReadOnlyCollection”
public static ReadOnlyCollection<object> meth1(this IWebElement element, IWebDriver driver, string strVerify)
{
return (ReadOnlyCollection<object>)((IJavaScriptExecutor)driver).ExecuteScript(string.Format( " var x = $(arguments[0]).find(\":contains('{0}')\"); return x;", strVerify), element);
}
methd2 在返回对象的 ReadOnlyCollection 时给出以下错误
“无法投射‘System.Collections.ObjectModel.ReadOnlyCollection 1[System.Object]' to type 'System.Collections.ObjectModel.ReadOnlyCollection
1[OpenQA.Selenium.IWebElement]’类型的对象。”}
public static ReadOnlyCollection<IWebElement> methd2(this IWebElement element, IWebDriver driver, string strVerify)
{
return (ReadOnlyCollection<IWebElement>)((IJavaScriptExecutor)driver).ExecuteScript(string.Format(" var x = $(arguments[0]).find(\":contains('{0}')\"); return x;", strVerify), element);
}
我如何编写一种方法来处理这两个输出。
我真的很感激这方面的任何帮助。提前致谢
解决方案
推荐阅读
- jsf-2 - JSF 2.2 ViewScoped bean 的行为就好像它是 WebSphere Portal 中的 SessionScoped bean
- excel - 从 VBA 中的多个表中清除数据
- jenkins - 从 Linux 在 IIS 上发布 NetCore 2.2
- javascript - location.reload(true); 不在ie11中
- google-apps-script - 如何创建可以创建新 Google Doc 的 Google Web App,然后在浏览器选项卡中打开它?
- tensorflow - 如何在 tensorflow.js 模型中添加图像并针对给定的图像标签训练模型
- terminal - 启动终端时applescript超时
- sql - 如何检查连接表中的完全匹配
- google-analytics - 谷歌分析点击呼叫事件到 AMP
- css - 更改primeng日历中时间选择器的样式