Search code examples
c#.netwatin

fetching only one url in c#


I am working on collecting urls from the web site in C# using WatiN framework. In my program it is fetching only one url. I don't know what is the problem. Any help will be appreciated.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WatiN.Core;
using WatiN.Core.Native.InternetExplorer;

namespace magicbricks
{
    class scroll
    {
        [STAThread]
        static void Main(string[] args)
        {
            Browser browserInstance;
            browserInstance = new IE(@"http://www.99acres.com/property-in-chennai-  ffid?search_type=QS&search_location=CP32&lstAcn=CP_R&lstAcnId=32&src=CLUSTER&isvoicesearch=N&keyword_suggest=chennai%20%28all%29%3B&fullSelectedSuggestions=chennai%20%28all%29&strEntityMap=W3sidHlwZSI6ImNpdHkifSx7IjEiOlsiY2hlbm5haSAoYWxsKSIsIkNJVFlfMzIsIFBSRUZFUkVOQ0VfUywgUkVTQ09NX1IiXX1d&texttypedtillsuggestion=chennai&refine_results=Y&Refine_Localities=Refine%20Localities&action=%2Fdo%2Fquicksearch%2Fsearch&suggestion=CITY_32%2C%20PREFERENCE_S%2C%20RESCOM_R");

            foreach (var links in browserInstance.Links.Filter(Find.ByClass("b")))
            {
                Console.WriteLine(links.Url);
                String filePath = "C:/Users/User/Desktop/New folder";
                String fileName = "newop4.csv";

                using (StreamWriter sr = new StreamWriter(Path.Combine(filePath, fileName), true))
                    {
                        sr.WriteLine(links.Url);
                    }
                    Console.ReadLine();
                }
            }
        }
    }

the above code prints only one url in the console.


Solution

  • Remove the Console.ReadLine(); As you are in a ForEach loop. If you still want the Console.ReadLine(); move it out the foreach

    The Console.ReadLine(); waits for a user input, after you enter any value you should see the next URL.