Beispiel #1
0
        private static void LoginUser(OpenQA.Selenium.PhantomJS.PhantomJSDriver driver, LoginInfo login)
        {
            driver.Navigate().GoToUrl("https://login.one.com/cp/");
            var username = driver.FindElement(By.Name("displayUsername"));

            username.SendKeys(login.UserName);

            var password = driver.FindElement(By.ClassName("password"));

            password.SendKeys(login.Password);

            var button = driver.FindElement(By.ClassName("oneButton"));

            button.Click();
        }
Beispiel #2
0
        private static List <string> GetTags(OpenQA.Selenium.PhantomJS.PhantomJSDriver driver)
        {
            var tElem = driver.FindElement(By.ClassName("tags"));
            var tags  = new List <string>();

            foreach (var a in tElem.FindElements(By.TagName("a")))
            {
                tags.Add(a.GetAttribute("text"));
            }

            return(tags);
        }
Beispiel #3
0
        protected override List <Packet> FetchPackets(Account account)
        {
            var tick = DateTime.Now.Ticks;

            // open page
            try
            {
                driver = Seeker.Service.GetDriver();
                Teller.Log($"[{account.Name}] 페이지를 초기화...");
                driver.Manage().Window.Size = new Size(765, 1000);
                driver.Navigate().GoToUrl("https://obank1.kbstar.com/quics?page=C025255&cc=b028364:b028702");// fill the form

                Teller.Log($"[{account.Name}] 로딩이 끝나기를 기다림...");
                var timeoutSeconds = account.IntervalSeconds;
                if (timeoutSeconds < 30)
                {
                    timeoutSeconds = 30;
                }
                var wait    = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutSeconds));
                var loading = By.Id("loading");
                wait.Until(ExpectedConditions.InvisibilityOfElementLocated(loading));

                Teller.Log($"[{account.Name}] 계좌 정보 입력중...");
                var account_num = By.Id("account_num");
                wait.Until(ExpectedConditions.ElementToBeClickable(account_num));
                driver.FindElement(account_num).SendKeys(account.Number.Replace("-", ""));

                var user_id = By.Id("user_id");
                wait.Until(ExpectedConditions.ElementToBeClickable(user_id));
                driver.FindElement(user_id).SendKeys(account.UserId);

                new SelectElement(driver.FindElementById("조회시작년")).SelectByValue(account.From.ToString("yyyy"));
                new SelectElement(driver.FindElementById("조회시작월")).SelectByValue(account.From.ToString("MM"));
                new SelectElement(driver.FindElementById("조회시작일")).SelectByValue(account.From.ToString("dd"));
                new SelectElement(driver.FindElementById("조회끝년")).SelectByValue(account.To.ToString("yyyy"));
                new SelectElement(driver.FindElementById("조회끝월")).SelectByValue(account.To.ToString("MM"));
                new SelectElement(driver.FindElementById("조회끝일")).SelectByValue(account.To.ToString("dd"));

                // open password keypad
                Teller.Log($"[{account.Name}] 비밀번호 키패드 이미지 추출...");
                var password = By.Id("비밀번호");
                wait.Until(ExpectedConditions.ElementToBeClickable(password));
                driver.FindElement(password).Click();

                // save keypad image
                var keypadImagePath = ContentManager.getPath($@"KB/{tick}.keypad.bmp");
                var keypad          = By.CssSelector(".keypadWrap img");
                wait.Until(ExpectedConditions.ElementToBeClickable(keypad));
                driver.GetScreenshot().SaveAsFile(keypadImagePath, ScreenshotImageFormat.Bmp);

                using (var bitmapKeypad = Bitmap.FromFile(keypadImagePath))
                {
                    // analyze keypad image
                    var element = driver.FindElement(keypad);
                    var area    = new
                    {
                        left   = element.Location.X,
                        center = element.Location.X + element.Size.Width / 2,
                        right  = element.Location.X + element.Size.Width,
                        width  = element.Size.Width,
                        top    = element.Location.Y,
                        height = element.Size.Height,
                    };
                    var btnLocations = new Dictionary <string, Point>()
                    {
                        { "num1", new Point(area.left + 46, area.top + 70) },
                        { "num2", new Point(area.center, area.top + 70) },
                        { "num3", new Point(area.right - 46, area.top + 70) },
                        { "num4", new Point(area.left + 46, area.top + 130) },
                        { "xxx1", new Point(area.center, area.top + 130) },
                        { "num6", new Point(area.right - 46, area.top + 130) },
                        { "xxx2", new Point(area.left + 46, area.top + 188) },
                        { "xxx3", new Point(area.center, area.top + 188) },
                        { "xxx4", new Point(area.right - 46, area.top + 188) },
                        { "delOne", new Point(area.left + 46, area.top + 246) },
                        { "xxx5", new Point(area.center, area.top + 246) },
                        { "delAll", new Point(area.right - 46, area.top + 246) },
                        { "submit", new Point(area.center, area.top + 300) },
                    };

                    // SOLUTION IMAGES' digests
                    var SIGMA      = 1;
                    var GAMMA      = 1.5;
                    var DEGREE     = 180;
                    var solDigests = new Dictionary <string, Digest>()
                    {
                        { "num5", ImagePhash.ComputeDigest(ContentManager.getPath(@"KB/KB_keypad_sol_5.bmp"), SIGMA, GAMMA, DEGREE) },
                        { "num7", ImagePhash.ComputeDigest(ContentManager.getPath(@"KB/KB_keypad_sol_7.bmp"), SIGMA, GAMMA, DEGREE) },
                        { "num8", ImagePhash.ComputeDigest(ContentManager.getPath(@"KB/KB_keypad_sol_8.bmp"), SIGMA, GAMMA, DEGREE) },
                        { "num9", ImagePhash.ComputeDigest(ContentManager.getPath(@"KB/KB_keypad_sol_9.bmp"), SIGMA, GAMMA, DEGREE) },
                        { "num0", ImagePhash.ComputeDigest(ContentManager.getPath(@"KB/KB_keypad_sol_0.bmp"), SIGMA, GAMMA, DEGREE) },
                    };

                    // find each digit of btns by correlation with solution image
                    Teller.Log($"[{account.Name}] 번호별로 이미지 추출, 해싱을 통해 상관관계 분석...");
                    var solBtnLocations = new Dictionary <string, Point>();
                    foreach (var btnLocation in btnLocations)
                    {
                        // compare only xxx btns (order shuffled btns 5,7,8,9,0)
                        if (!btnLocation.Key.StartsWith("xxx"))
                        {
                            solBtnLocations.Add(btnLocation.Key, btnLocation.Value);
                            continue;
                        }

                        // crop image
                        var btnImagePath = ContentManager.getPath($@"KB/{tick}.{btnLocation.Key}.bmp");
                        var rect         = new Rectangle(btnLocation.Value.X - 22, btnLocation.Value.Y - 22, 44, 44);
                        using (var bitmap = new Bitmap(rect.Width, rect.Height))
                            using (var graphic = Graphics.FromImage(bitmap))
                            {
                                graphic.DrawImage(bitmapKeypad, 0, 0, rect, GraphicsUnit.Pixel);
                                bitmap.Save(btnImagePath);

                                // find each digit for btns
                                var    btnDigest  = ImagePhash.ComputeDigest(btnImagePath, SIGMA, GAMMA, DEGREE);
                                string answerKey  = null;
                                double answerCorr = 0;
                                foreach (var solDigest in solDigests)
                                {
                                    var corr = ImagePhash.GetCrossCorrelation(btnDigest, solDigest.Value);
                                    if (corr > answerCorr)
                                    {
                                        answerCorr = corr;
                                        answerKey  = solDigest.Key;
                                    }
                                }

                                // add to solved btn locations
                                try
                                {
                                    solBtnLocations.Add(answerKey, btnLocation.Value);
                                    Teller.Log($"[{account.Name}] {answerKey} 분석 완료...");
                                }
                                catch (ArgumentException)
                                {
                                    Teller.Log($"[{account.Name}] {btnLocation.Key}를 확정 할 수 없음... 재시작");
                                    throw new NeedToRefetchError();
                                }
                            }
                    }

                    // now tocuh the keypad as the solution
                    Point locationTo;
                    var   zero = driver.FindElement(password);
                    var   dx   = -area.left + 90;
                    var   dy   = -area.top + 15;

                    //                        // helper to adjust dx,dy
                    //                        driver.ExecuteScript(@"
                    //window.onclick = function(e) {
                    //    var d = document.createElement('div');
                    //    d.style.width='2px'; d.style.height='2px';
                    //    d.style.position='absolute';
                    //    d.style.display='block';
                    //    d.style.top=e.clientY+'px';
                    //    d.style.left=e.clientX+'px';
                    //    d.style.background='red';
                    //    d.style.zIndex='10000000';
                    //    document.body.appendChild(d);
                    //};
                    //                        ");

                    foreach (var digit in account.Password.ToCharArray())
                    {
                        locationTo = solBtnLocations["num" + digit];
                        new Actions(driver).MoveToElement(zero).MoveByOffset(locationTo.X + dx, locationTo.Y + dy).Click().Perform();
                        Teller.Log($"[{account.Name}] 번호 ({locationTo.X + dx}, {locationTo.Y + dy}) 클릭...");
                    }
                    locationTo = btnLocations["submit"]; // (373, 554).
                    new Actions(driver).MoveToElement(zero).MoveByOffset(locationTo.X + dx, locationTo.Y + dy).Click().Perform();
                    Teller.Log($"[{account.Name}] 확인 ({locationTo.X + dx}, {locationTo.Y + dy}) 클릭...");


                    // submit the form
                    wait.Until(ExpectedConditions.InvisibilityOfElementLocated(keypad));
                    var submit = By.CssSelector("input[type=submit]");
                    driver.FindElement(submit).Click();
                }


                // check page loaded
                var table = By.CssSelector(".tType01");
                wait.Until(ExpectedConditions.ElementIsVisible(table));

                // reprocessing as Packet def
                List <Packet> packets = new List <Packet>();
                for (var pageNum = 0; true; pageNum++)
                {
                    Teller.Log($"[{account.Name}] {pageNum + 1} 페이지 파싱 및 분석...");
                    //driver.GetScreenshot().SaveAsFile(ContentManager.getPath($@"KB/KB_result_{pageNum}.bmp"), ScreenshotImageFormat.Bmp);

                    var trs = driver.FindElementsByCssSelector(".tType01 tbody tr");
                    if (trs.Count < 2)
                    {
                        Teller.Log($"[{account.Name}] {pageNum + 1} 페이지 거래 내역 없음. 페이지 종료...");
                        break; // No items
                    }
                    for (var i = 0; i < trs.Count; i++)
                    {
                        var tr = trs[i];
                        if (i % 2 == 0)
                        {
                            var datetimeTemp = tr.FindElement(By.CssSelector("td:nth-child(1)")).GetAttribute("textContent").Trim();
                            datetimeTemp = datetimeTemp.Substring(0, 10) + " " + datetimeTemp.Substring(10);
                            var packet = new Packet
                            {
                                Date      = Convert.ToDateTime(datetimeTemp),
                                Note      = tr.FindElement(By.CssSelector("td:nth-child(2)")).GetAttribute("textContent").Trim(),
                                OutName   = tr.FindElement(By.CssSelector("td:nth-child(3)")).GetAttribute("textContent").Trim(),
                                OutAmount = Convert.ToDecimal(tr.FindElement(By.CssSelector("td:nth-child(4)")).GetAttribute("textContent").Trim()),
                                InAmount  = Convert.ToDecimal(tr.FindElement(By.CssSelector("td:nth-child(5)")).GetAttribute("textContent").Trim()),
                                Balance   = Convert.ToDecimal(tr.FindElement(By.CssSelector("td:nth-child(6)")).GetAttribute("textContent").Trim()),
                                Bank      = tr.FindElement(By.CssSelector("td:nth-child(7)")).GetAttribute("textContent").Trim(),
                                Type      = tr.FindElement(By.CssSelector("td:nth-child(8)")).GetAttribute("textContent").Trim(),
                            };
                            packets.Add(packet);
                        }
                        else
                        {
                            packets[(i - 1) / 2].InName = tr.GetAttribute("textContent").Trim();
                        }
                    }

                    // for pagination
                    try
                    {
                        driver.FindElementByCssSelector(".optionBtnArea .leftArea .next input").Click();
                        wait.Until(ExpectedConditions.ElementIsVisible(By.Id("loading")));
                        wait.Until(ExpectedConditions.InvisibilityOfElementLocated(By.Id("loading")));
                    }
                    catch (NoSuchElementException)
                    {
                        Teller.Log($"[{account.Name}] 페이지 종료...");
                        break;
                    }
                }

                return(packets);
            }
            catch (WebDriverException e)
            {
                Teller.Log($"[{account.Name}] 가상 브라우저 비정상 종료...\n{e.Message}");
                throw new NeedToRefetchError();
            }
            catch (NeedToRefetchError)
            {
                throw;
            }
            catch (Exception e)
            {
                Teller.Log($"[{account.Name}] 처리되지 않은 예외 발생...\n{e.Message}");
                throw new NeedToRefetchError();
            }
            finally
            {
                // delete temp files
                DisposeFile($@"KB/{tick}.keypad.bmp");
                DisposeFile($@"KB/{tick}.xxx1.bmp");
                DisposeFile($@"KB/{tick}.xxx2.bmp");
                DisposeFile($@"KB/{tick}.xxx3.bmp");
                DisposeFile($@"KB/{tick}.xxx4.bmp");
                DisposeFile($@"KB/{tick}.xxx5.bmp");
                Dispose();
            }
        }
Beispiel #4
0
        private static void GoToEmailOverview(OpenQA.Selenium.PhantomJS.PhantomJSDriver driver)
        {
            var emailLink = driver.FindElement(By.Id("frontpageMailLink"));

            emailLink.Click();
        }
Beispiel #5
0
        static void Main(string[] args)
        {
            //credit to jimevans over at #selenium! :D
            IWebDriver driver = new OpenQA.Selenium.PhantomJS.PhantomJSDriver();
            Dictionary<string, KeyValuePair<int, double>> cards = new Dictionary<string, KeyValuePair<int, double>>();
            driver.Url = "http://steamcommunity.com/market/search?q=appid%3A753+trading+card";
            var nextPage = driver.FindElement(By.Id("searchResults_btn_next"));
            string classAttribute = nextPage.GetAttribute("class");
            int i = 1;
            while (classAttribute != null && !classAttribute.Contains("disabled"))
            {
                File.WriteAllText("f" + i.ToString() + ".html", driver.PageSource);
                var elementList = driver.FindElements(By.CssSelector("div.market_listing_row.market_recent_listing_row"));
                if (elementList.Count == 0)
                {
                    int h = 6;
                    break;
                }
                Console.WriteLine("Done page {0} with {1} elements", i, elementList.Count());
                ++i;
                foreach (var element in elementList)
                {
                    var itemNameElement = element.FindElement(By.CssSelector("span.market_listing_item_name"));
                    var gameNameElement = element.FindElement(By.CssSelector("span.market_listing_game_name"));
                    if (gameNameElement.Text.Contains("Emoticon"))
                        continue;
                    var gameName = gameNameElement.Text.Replace(" Trading Card", "");

                    var priceElement = element.FindElement(By.CssSelector("div.market_listing_right_cell.market_listing_num_listings"));
                    string priceWithJunk = priceElement.Text;
                    priceWithJunk = priceWithJunk.Substring(priceWithJunk.IndexOf('$') + 1);
                    double price = Convert.ToDouble(priceWithJunk.Remove(priceWithJunk.IndexOf('U')).Trim());
                    //Console.WriteLine("Item: {0}, Game:{1}, Price: {2}", itemNameElement.Text, gameName, price);
                    if (cards.Keys.Contains(gameName)) //add 1
                    {
                        cards[gameName] = new KeyValuePair<int, double>(cards[gameName].Key + 1, cards[gameName].Value + price);
                    }
                    else
                    {
                        cards[gameName] = new KeyValuePair<int, double>(1, price);
                    }
                }

                // Note: you might be able to get away with not clicking on this
                // element and using a direct navigation.
                nextPage.Click();
                WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
                try
                {
                    wait.Until<bool>((d) =>
                    {
                        try
                        {
                            // This will wait until the listing item name element goes
                            // "stale", meaning it's been removed from the DOM and replaced
                            // by a similar element on the next page.
                            bool dummy = elementList[0].FindElement(By.CssSelector("span.market_listing_item_name")).Displayed;
                            return false;
                        }
                        catch (StaleElementReferenceException)
                        {
                            return true;
                        }
                    });
                }
                catch (WebDriverTimeoutException)
                {
                    break;
                }
            }

            driver.Quit();

            /*var driver = new OpenQA.Selenium.PhantomJS.PhantomJSDriver();
            //var driver = new OpenQA.Selenium.Firefox.FirefoxDriver();
            string lastFirstElement = "novalue", currFirstElement="novalue";
            for (int i = 1; i < 111; ++i)
            {
                //load the page
                string url = "http://steamcommunity.com/market/search?q=appid%3A753+trading+card#p" + i.ToString();
                driver.Url = url;
                driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 15));
                HtmlDocument doc = new HtmlDocument();
                WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0, 1, 0));

                wait.Until(d => d.FindElement(By.Id("searchResultsTable")));
                //try to find the cards
                IEnumerable<HtmlNode> findThings = null;
                do
                {

                    if (i == 1)
                    {
                        System.Threading.Thread.Sleep(5000);
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(1000);
                    }
                    string s = driver.PageSource;
                    doc.LoadHtml(s);
                    findThings = doc.DocumentNode.Descendants("div").Where(d => d.Attributes.Contains("class") &&
                        d.Attributes["class"].Value == "market_listing_row market_recent_listing_row");
                    currFirstElement = findThings.First().InnerText;

                } while (lastFirstElement.Equals(currFirstElement) && i != 1);

                //reset
                //TODO timeout
                lastFirstElement = currFirstElement;
                 Console.WriteLine("Done page" + i);

                foreach (var thing in findThings)
                {
                    string name = thing.Descendants("span").Where(d => d.Attributes.Contains("class") &&
                        d.Attributes["class"].Value == "market_listing_item_name").First().InnerHtml;
                    string type = thing.Descendants("span").Where(d => d.Attributes.Contains("class") &&
                        d.Attributes["class"].Value == "market_listing_game_name").First().InnerHtml.Replace(" Trading Card", "");
                    string priceHtml = thing.Descendants("div").Where(d => d.Attributes.Contains("class") &&
                        d.Attributes["class"].Value == "market_listing_right_cell market_listing_num_listings").First().InnerHtml;
                    string priceWithJunk = priceHtml.Substring(priceHtml.IndexOf('$') + 1);
                    double price = Convert.ToDouble(priceWithJunk.Remove(priceWithJunk.IndexOf('U')).Trim());

                    //if(type.Contains("Walking Dead"))
                    Console.WriteLine("Page {0} adding {1} from {2}", i, name, type);

                }
            }

            driver.Quit();*/

            List<string> print = new List<string>();
            foreach(KeyValuePair<string, KeyValuePair<int, double>> cardset in cards)
            {
                print.Add(string.Join(",", cardset.Key, cardset.Value.Key, cardset.Value.Value));
            }
            File.WriteAllLines("print.csv", print);
            int j = 4;
        }
Beispiel #6
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;
        }