首页 > 解决方案 > 如何在 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.ReadOnlyCollection1[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);
    }

我如何编写一种方法来处理这两个输出。

我真的很感激这方面的任何帮助。提前致谢

标签: javascriptc#selenium

解决方案


推荐阅读