public void TestMethod1()
        {
            var browser = new OpenQA.Selenium.PhantomJS.PhantomJSDriver();

            var dir = Directory.CreateDirectory(@"c:\temp\news");

            for (int i = 0; i < 1000; i++)
            {
                browser.Navigate().GoToUrl("http://webadvice.no/svada");

                var svada = browser.FindElementById("content").FindElement(By.ClassName("wrap"));

                using (var reader = new StringReader(svada.Text))
                {
                    reader.ReadLine(); // header
                    //reader.ReadLine();

                    var title = reader.ReadLine();
                    var ingress = reader.ReadLine();
                    var content = new StringBuilder();

                    var iterations = 0;

                    while (reader.Peek() != -1)
                    {
                        string header = "";
                        reader.ReadLine(); // br
                        if (iterations++ < 5)
                        {
                            header = "<h3>" + reader.ReadLine() + "</h3>";
                        }
                        var paragraph = reader.ReadLine();

                        content.AppendFormat("{0}<p>{1}</p>", header, paragraph);
                    }

                    Console.Out.WriteLine("Title: " + title);
                    Console.Out.WriteLine("Ingress: " + ingress);
                    Console.Out.WriteLine("Body: " + content);

                    using (var writer = new StreamWriter(@"c:\temp\news\news" + i + ".txt"))
                    {
                        writer.WriteLine(title);
                        writer.WriteLine(ingress);
                        writer.WriteLine(content);
                    }

                }
            }

            browser.Close();
        }
Beispiel #2
0
        public Location GetLocation(string urlRe, string province, string district, string pacelNo)
        {
            var result = new Location();
            var parcel = 0;

            if (string.IsNullOrEmpty(province) || string.IsNullOrEmpty(district) || string.IsNullOrEmpty(pacelNo))
            {
                return result;
            }

            if (pacelNo.Contains("-"))
            {
                var stat = int.TryParse(pacelNo.Split('-')[0], out parcel);
                if (!stat)
                    return result;
            }
            else
            {
                var stat = int.TryParse(pacelNo, out parcel);
                if (!stat)
                    return result;
            }

            var loc = DataHelper.GetLocation(province, district, parcel);
            if (loc != null)
            {
                return loc;
            }

            string driverPath = @"D:\MyProjects\FRES\src\FRES.Source.Map\Drivers\";
            IWebDriver driver = new OpenQA.Selenium.PhantomJS.PhantomJSDriver(driverPath);
            //IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(driverPath);
            try
            {
                var url = "http://dolwms.dol.go.th/tvwebp/";
                driver.Navigate().GoToUrl(url);
                var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(TIMEOUT));

                Thread.Sleep(DELAY);

                var ddlProvince = driver.FindSelectElementWhenPopulated(By.Name("ddlProvince"), TIMEOUT);
                ddlProvince.SelectBySubText(province);

                Thread.Sleep(DELAY);

                var ddlAmphur = driver.FindSelectElementWhenPopulated(By.Name("ddlAmphur"), TIMEOUT);
                ddlAmphur.SelectBySubText(district);

                Thread.Sleep(DELAY);

                var txtPacelNo = new WebDriverWait(driver, TimeSpan.FromSeconds(TIMEOUT)).Until(ExpectedConditions.ElementExists(By.Name("txtPacelNo")));
                txtPacelNo.SendKeys(pacelNo);

                Thread.Sleep(DELAY);

                var btnFind = driver.FindElement(By.Name("btnFind"));
                IJavaScriptExecutor js = driver as IJavaScriptExecutor;
                js.ExecuteScript("arguments[0].click();", btnFind);

                Thread.Sleep(DELAY);
                //var element = new WebDriverWait(driver, TimeSpan.FromSeconds(3)).Until(ExpectedConditions.TextToBePresentInElement(driver.FindElement(By.Id("ddlAmphur")), "01"));
                //wait.Until(ExpectedConditions.ElementExists(By.CssSelector("div[style=\"transform: translateZ(0px); position: absolute; left: 0px; top: 0px; z-index: 107; width: 100%;\"]")));

                var isExist = wait.Until((d) => { return driver.PageSource.Contains("createMarker( new Array("); });

                if (isExist)
                {
                    var html = driver.PageSource;
                    html = GetStrBtw(html, "createMarker( new Array(", "));");//.Replace("'", string.Empty);
                    var dtls = html.Split(',').Select(x => x.Replace("'", "")).ToArray();
                    result = new Location
                    {
                        Amphur = district,
                        Province = province,
                        ParcelCode = parcel,
                        Lat = double.Parse(dtls[7]),
                        Lon = double.Parse(dtls[8])
                    };

                    DataHelper.InsertLocation(result);
                }
                else
                {
                    throw new Exception("Can't find location");
                }
            }
            catch (Exception ex)
            {
                lock (sync)
                    File.AppendAllText("D:/RE/M.log", DateTime.Now.ToString("yyyyMMdd HH:mm") + "," + province + "," + district + "," + parcel + "," + urlRe + "," + ex.GetBaseException().Message + "\r\n");
            }
            finally
            {
                //Thread.Sleep(3000);
                driver.Close();
                driver.Quit();
                driver.Dispose();
            }
            return result;
        }