user18762817
user18762817

Reputation:

C# Selenium, getting text of Invisible elements is slow

I am trying to get invisible text that is on a given page using C# Selenium EdgeDriver. I am able to do it, i.e. I get the elements (such as within span, p or b tags, then I am filtering out the elements into a list based on Displayed property, and finally I am calling GetAttribute("textContent") to get the text. The problem I am having is that is slow, about 10 seconds for the page I am doing that on, do you think there is any better way, or making this faster?

Thanks,

       public static string GetInvisibleText()
        {
            Stopwatch s500 = new Stopwatch();
            s500.Start();

            string returnable = "\r\n";
            var elements = driver.FindElements(By.XPath("//b | //span | //p | //a | //h1 | //h2 | //h3 | //h4 | //h5 | //h6 | //div"));
            List<string> list = new List<string>();
            var displayed_elements = elements.Where(e => !e.Displayed);

            foreach(var el in displayed_elements)
            {
                try
                {
                    string val = el.GetAttribute("textContent");
                    val = val.Trim();
                    val = Regex.Replace(val, @"\s+", " ");
                    list.Add(val);
                }
                catch (Exception ex)
                {

                }
            }

            list = list.Distinct(StringComparer.OrdinalIgnoreCase).ToList();

            foreach (string line in list)
            {
                returnable = returnable + line + "\r\n";
            }

            s500.Stop();
            return returnable;
        }

Upvotes: 0

Views: 77

Answers (0)

Related Questions