none
How to alert text content in console RRS feed

  • Question

  • Hello guys this is my sourcecode

    main.container-fluid:nth-child(5)<main class="container-fluid ng-scope" ng-view=""><div class="row ng-scope">
        <section class="col-md-8 col-md-offset-2 col-sm-10 col-sm-offset-1">
            <h3 ng-show="searchQuery" class="page-header page-header-sm ng-hide"><span translate="TITLE_SEARCH_RESULTS" class="ng-scope">Search Results</span> <span class="label label-default ng-binding" ng-bind-html="searchQuery"></span></h3>
            <h3 ng-show="!searchQuery" class="page-header page-header-sm ng-scope" translate="TITLE_ALL_PRODUCTS">All Products</h3>
    
            <div class="alert-info ng-hide" ng-show="confirmation">
                <p class="ng-binding"></p>
            </div>
        </section>
    </div></main>

    Now I need a loop that checking for the value"All Products", then write the content in console

    This is my code I made

     IList<IWebElement> links = Driver.Instance.FindElements(By.XPath("//h3[@class='page-header page-header-sm ng-scope']"));
             
                    IList<IWebElement> listOflinks = new List<IWebElement>();
    
                    for (int i = 0; i < links.Count; i++)
                    {
                        links = Driver.Instance.FindElements(By.XPath("//h3[@class='page-header page-header-sm ng-scope']"));
    
                        if (!string.IsNullOrEmpty(links[i].Text))
                            //links[i].Click();
                            Console.WriteLine(links[i].ToString());
                    }

    though I just get as result

    Element (id = c25b9ecd-b828-4353-a9d8-9f8a53b9cee9)

    Could someone help me ot correct this?

    Thank you in advance

    Monday, January 8, 2018 6:22 PM

All replies

  • //you only need to replace this line in your code

    Console.WriteLine(links[i].Text.ToString());

    Best Regards,
    Govind Chouksey

    Please click "Mark as Answer" if this response resolved your issue.


    Tuesday, January 9, 2018 5:56 AM
  • I get exactly the same response as before.

    used following code:

         IList<IWebElement> links = Driver.Instance.FindElements(By.XPath("//h3[@class='page-header page-header-sm ng-scope']"));
             
                    IList<IWebElement> listOflinks = new List<IWebElement>();
    
                    for (int i = 0; i < links.Count; i++)
                    {
                        links = Driver.Instance.FindElements(By.XPath("//h3[@class='page-header page-header-sm ng-scope']"));
    
                        if (!string.IsNullOrEmpty(links[i].Text))
    
                      Console.WriteLine(links[i].Text.ToString());
    
                    }
    

    • Edited by Elmnas Tuesday, January 9, 2018 4:59 PM
    Tuesday, January 9, 2018 4:57 PM
  • With all due respect, I think you must have made a mistake when making the change.  The IWebElement.Text property is a string.  You should need nothing more complicated than this:

        Console.WriteLine( links[i].Text );

    By the way, you do not need to refetch the list of elements every time you go through the loop.  You already fetch "links" outside the loop.


    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Tuesday, January 9, 2018 6:15 PM
  • Hello Elmnas,

    Try the below code:

    Console.WriteLine(links[i].GetAttribute("innerHTML"));

    This works well on my side.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 10, 2018 9:44 AM
    Moderator
  • So how should the code be instead then?

    thank you in advance Tim Roberts.
    Friday, January 12, 2018 5:52 PM
  • Hello Elmnas,

    Console.WriteLine(links[i].GetAttribute("innerHTML"));

    The "innerHTML" attribute will contain all information of childe DOM Object, which is a paragraph of html text.  If you want to parse it, You just need XML parse object. .Net has provided a lot of ways to parse XML text. like XmlDocument, linq to Xml or others. I can't assume what info you want to exact. The following is my testing code.

    var driver = new InternetExplorerDriver(@"D:\Test7\T1");
    
    driver.Navigate().GoToUrl(@http://xxx.xxxx.xxx);
    
    IWebElement demoDiv = driver.FindElement(By.Id("u1"));
    
    Console.WriteLine("----------------------------");
    Console.WriteLine(demoDiv.ToString());
    
    Console.WriteLine("----------------------------");
    Console.WriteLine(demoDiv.GetAttribute("innerHTML"));

    And if your issue has been solved, please don't forget to mark the helpful reply as answer.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 16, 2018 8:31 AM
    Moderator
  • Hello Elmnas,

    Is there any update or any other assistance I could provide? You could mark the helpful reply as answer if the issue has been solved. And if you have any concerns, please do not hesitate to let us know.

    Thank you for your understanding and cooperation.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sunday, January 28, 2018 12:57 PM
    Moderator