C#Selenium - innerHTML全部捕获

时间:2014-07-24 19:15:11

标签: c# javascript selenium

大家好我有这个方法:

    public List<string> captureBySingleString(By by)
    {
            List<string> capturedList = new List<string>();
            IWebElement[] List = driver.FindElements(by).ToArray();
            for (int i = 0; i < List.Length; i++)
            {
                if (List[i].Text.Contains("\r\n"))
                {
                    string[] lines = Regex.Split(List[i].Text, "\r\n");
                    List<string> tempCapturedList = lines.ToList();
                    capturedList.AddRange(tempCapturedList);
                }
                else
                {
                    capturedList.Add(List[i].Text);
                }
            }
            return capturedList;
    }

基本上采用by元素并在该上下文中抓取所有内容。例如,如果捕获了"CASE VIEWS:\r\nSource\r\nCanadian Criminal Cases\r\nDominion Law Reports",则返回列表中会有{"CASE VIEWS", "Source", "Canadian Criminal Cases", "Dominion Law Reports"}

唯一的问题是我需要innerHTML,因为我需要HTML代码。例如,我需要使用HTML标记捕获"<strong>CASE VIEWS:</strong>\r\nSource\r\nCanadian Criminal Cases\r\nDominion Law Reports"。我是这个方法innerHTML的奖励:

    public string innerHTML(By by)
    {
        IWebElement element = driver.FindElement(by);
        IJavaScriptExecutor js = driver as IJavaScriptExecutor;
        string htmlCode = (string)js.ExecuteScript("return arguments[0].innerHTML;", element);
        htmlCode = htmlCode.Replace("\"", "'");
        return htmlCode;
    }

我们如何一起实施这两种方法?所以我可以捕获BySingleString,但也捕获innerHTML代码。

0 个答案:

没有答案