Beispiel #1
0
        private static string SlideUseChrome(string companyName)
        {
            const string url = "http://www.gsxt.gov.cn/index.html";

            //InternetExplorerOptions options = new InternetExplorerOptions();
            ////取消浏览器的保护模式 设置为true
            //options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
            //这里用chrome浏览器 ie浏览器有问题
            var options = new ChromeOptions();

            options.AddArguments("user-agent=Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25");
            string path = $"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\\chromeDriverData";

            FileHandler.DeleteFilesIfExistsDir(path);
            options.AddArguments($"user-data-dir={path}");
            using (var driver = new ChromeDriver(options))
            {
                //设置浏览器大小 设置为最大 元素的X,Y坐标就准了 不然就不准(不知道原因)
                driver.Manage().Window.Maximize();

                var navigation = driver.Navigate();
                navigation.GoToUrl(url);
                //等待时间
                var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
                //等待元素全部加载完成
                wait.Until(ExpectedConditions.ElementExists(By.Id("keyword")));
                var keyWord = driver.FindElement(By.Id("keyword"));
                //keyWord.SendKeys("温州红辣椒电子商务有限公司");
                keyWord.SendKeys(companyName);

                //等待元素全部加载完成
                wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("btn_query")));
                var js       = (IJavaScriptExecutor)driver;
                var btnQuery = driver.FindElement(By.Id("btn_query"));
                //经测试,这里要停一下,不然刚得到元素就click可能不会出现滑动块窗口(很坑的地方)
                Thread.Sleep(1000);
                js.ExecuteScript("arguments[0].click();", btnQuery);
                //btnQuery.Click();
                //btnQuery.SendKeys(Keys.Enter);

                //截图加滑动处理
                //因为只有一个弹出窗口,所以直接进到这个里面就好了
                var allWindowsId = driver.WindowHandles;
                if (allWindowsId.Count != 1)
                {
                    throw new Exception("多个弹出窗口。");
                }

                foreach (var windowId in allWindowsId)
                {
                    driver.SwitchTo().Window(windowId);
                }


                //等待元素全部加载完成
                wait.Until(ExpectedConditions.PresenceOfAllElementsLocatedBy(By.CssSelector("div.gt_box")));

                //找到图片
                var imageBox = driver.FindElement(By.CssSelector("div.gt_box"));
                //先休息一会,不然截图不对
                Thread.Sleep(1000);
                //截图得到子图片
                var imageFirst = GetSubImage(driver, imageBox);
                //imageFirst?.Save("c:/test.png");


                var slide  = driver.FindElement(By.CssSelector("div.gt_slider_knob.gt_show"));
                var action = new Actions(driver);
                //移到起始位置
                action.ClickAndHold(slide).MoveByOffset(0, 0).Perform();
                //先休息一会,不然截图不对
                Thread.Sleep(1000);
                //再截图得到子图片
                var imageSecond = GetSubImage(driver, imageBox);
                //imageSecond?.Save("c:/test1.png");

                var pass     = false;
                var tryTimes = 0;
                //试5次或者pass
                while (tryTimes++ < 5 && !pass)
                {
                    Console.WriteLine($"第{tryTimes}次。");
                    var left = SlideImageHandler.FindXDiffRectangeOfTwoImage(imageFirst, imageSecond) - 7;
                    Console.WriteLine($"减7后等于:{left}");
                    if (left <= 0)
                    {
                        throw new Exception("算出的距离小于等于0");
                    }
                    var pointsTrace = SlideImageHandler.GeTracePoints(left);

                    //移动
                    MoveHandler(pointsTrace, action, slide);
                    //这里不能暂停
                    //Thread.Sleep(TimeSpan.FromSeconds(3));

                    wait.Until(ExpectedConditions.PresenceOfAllElementsLocatedBy(By.XPath("//div[@class='gt_info_text']/span[@class='gt_info_type']")));
                    wait.Until(ExpectedConditions.ElementExists(By.XPath("//div[@class='gt_info_text']/span[@class='gt_info_type']")));
                    //找得到元素,但是它不在当前可见的页面上。
                    var infoText = driver.FindElement(By.XPath("//div[@class='gt_info_text']/span[@class='gt_info_type']")).Text;
                    if (infoText.Contains("验证通过"))
                    {
                        pass = true;
                    }
                    //如果判断为非人行为 刷新验证码 重新截图
                    else if (infoText.Contains("再来一次"))
                    {
                        Thread.Sleep(TimeSpan.FromSeconds(3));
                        wait.Until(ExpectedConditions.PresenceOfAllElementsLocatedBy(By.ClassName("gt_refresh_button")));
                        var refreshButtom = driver.FindElement(By.ClassName("gt_refresh_button"));
                        refreshButtom.Click();
                        //等待元素全部加载完成
                        wait.Until(ExpectedConditions.PresenceOfAllElementsLocatedBy(By.CssSelector("div.gt_box")));
                        //找到图片
                        imageBox = driver.FindElement(By.CssSelector("div.gt_box"));
                        //先休息一会,不然截图不对
                        Thread.Sleep(1000);
                        //截图得到子图片
                        imageFirst = GetSubImage(driver, imageBox);
                        //imageFirst?.Save("c:/test.png");
                        slide  = driver.FindElement(By.CssSelector("div.gt_slider_knob.gt_show"));
                        action = new Actions(driver);
                        //移到起始位置
                        action.ClickAndHold(slide).MoveByOffset(0, 0).Perform();
                        //先休息一会,不然截图不对
                        Thread.Sleep(1000);
                        //再截图得到子图片
                        imageSecond = GetSubImage(driver, imageBox);
                        //imageSecond?.Save("c:/test1.png");
                        Console.WriteLine("刷新图片。");
                        pass = false;
                    }
                    else
                    {
                        pass = false;
                    }
                    Console.WriteLine($"pass:{pass}。");
                    //先休息一会,不然截图不对
                    //Thread.Sleep(1000);
                    var imageThird = GetSubImage(driver, imageBox);
                    //imageThird?.Save("c:/test2.png");
                }


                Console.WriteLine(pass ? "验证通过。" : "验证失败。");

                if (!pass)
                {
                    throw new Exception("极速验证码验证失败。");
                }


                Thread.Sleep(TimeSpan.FromSeconds(1));

                //得到页面内容
                return(driver.PageSource);
            }
        }
Beispiel #2
0
        /// <summary>
        /// SlideWithPhantomJs
        /// </summary>
        /// <param name="companyName"></param>
        private static string SlideUsePhantomJs(string companyName)
        {
            const string url = "http://www.gsxt.gov.cn/index.html";

            var userAgent =
                @"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36";
            var options = new PhantomJSOptions();

            options.AddAdditionalCapability(@"phantomjs.page.settings.userAgent", userAgent);

            using (var driver = new PhantomJSDriver(options))
            {
                //设置浏览器大小 设置为最大 元素的X,Y坐标就准了 不然就不准(不知道原因)
                driver.Manage().Window.Maximize();

                var navigation = driver.Navigate();
                navigation.GoToUrl(url);
                //等待时间
                var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
                //等待元素全部加载完成
                wait.Until(ExpectedConditions.ElementExists(By.Id("keyword")));
                var keyWord = driver.FindElement(By.Id("keyword"));
                //keyWord.SendKeys("温州红辣椒电子商务有限公司");
                keyWord.SendKeys(companyName);

                //等待元素全部加载完成
                wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("btn_query")));
                var js       = (IJavaScriptExecutor)driver;
                var btnQuery = driver.FindElement(By.Id("btn_query"));
                //经测试,这里要停一下,不然刚得到元素就click可能不会出现滑动块窗口(很坑的地方)
                Thread.Sleep(1000);
                js.ExecuteScript("arguments[0].click();", btnQuery);
                //btnQuery.Click();
                //btnQuery.SendKeys(Keys.Enter);

                //截图加滑动处理
                //因为只有一个弹出窗口,所以直接进到这个里面就好了
                var allWindowsId = driver.WindowHandles;
                if (allWindowsId.Count != 1)
                {
                    throw new Exception("多个弹出窗口。");
                }

                foreach (var windowId in allWindowsId)
                {
                    driver.SwitchTo().Window(windowId);
                }


                //等待元素全部加载完成
                wait.Until(ExpectedConditions.PresenceOfAllElementsLocatedBy(By.CssSelector("div.gt_box")));

                //找到图片
                var imageBox = driver.FindElement(By.CssSelector("div.gt_box"));
                //先休息一会,不然截图不对
                Thread.Sleep(1000);
                //截图得到子图片
                var imageFirst = GetSubImage(driver, imageBox);
                //imageFirst?.Save("c:/test.png");


                var slide  = driver.FindElement(By.CssSelector("div.gt_slider_knob.gt_show"));
                var action = new Actions(driver);
                //移到起始位置
                action.ClickAndHold(slide).MoveByOffset(0, 0).Perform();
                //先休息一会,不然截图不对
                Thread.Sleep(1000);
                //再截图得到子图片
                var imageSecond = GetSubImage(driver, imageBox);
                //imageSecond?.Save("c:/test1.png");

                var pass     = false;
                var tryTimes = 0;
                //试5次或者pass
                while (tryTimes++ < 5 && !pass)
                {
                    Console.WriteLine($"第{tryTimes}次。");
                    var left = SlideImageHandler.FindXDiffRectangeOfTwoImage(imageFirst, imageSecond) - 7;
                    Console.WriteLine($"减7后等于:{left}");
                    if (left <= 0)
                    {
                        throw new Exception("算出的距离小于等于0");
                    }
                    var pointsTrace = SlideImageHandler.GeTracePoints(left);

                    //移动
                    MoveHandler(pointsTrace, action, slide);

                    wait.Until(ExpectedConditions.PresenceOfAllElementsLocatedBy(By.XPath("//div[@class='gt_info_text']/span[@class='gt_info_type']")));
                    //找得到元素,但是它不在当前可见的页面上。
                    var infoText = driver.FindElement(By.XPath("//div[@class='gt_info_text']/span[@class='gt_info_type']")).Text;
                    if (infoText.Contains("验证通过"))
                    {
                        pass = true;
                    }
                    //如果判断为非人行为 刷新验证码 重新截图
                    else if (infoText.Contains("再来一次"))
                    {
                        wait.Until(ExpectedConditions.PresenceOfAllElementsLocatedBy(By.ClassName("gt_refresh_button")));
                        var refreshButtom = driver.FindElement(By.ClassName("gt_refresh_button"));
                        refreshButtom.Click();
                        //等待元素全部加载完成
                        wait.Until(ExpectedConditions.PresenceOfAllElementsLocatedBy(By.CssSelector("div.gt_box")));
                        //找到图片
                        imageBox = driver.FindElement(By.CssSelector("div.gt_box"));
                        //先休息一会,不然截图不对
                        Thread.Sleep(1000);
                        //截图得到子图片
                        imageFirst = GetSubImage(driver, imageBox);
                        //imageFirst?.Save("c:/test.png");
                        //移到起始位置
                        action.ClickAndHold(slide).MoveByOffset(0, 0).Perform();
                        //先休息一会,不然截图不对
                        Thread.Sleep(1000);
                        //再截图得到子图片
                        imageSecond = GetSubImage(driver, imageBox);
                        //imageSecond?.Save("c:/test1.png");
                        Console.WriteLine("刷新图片。");
                        pass = false;
                    }
                    else
                    {
                        pass = false;
                    }
                    Console.WriteLine($"pass:{pass}。");
                    //先休息一会,不然截图不对
                    //Thread.Sleep(1000);
                    var imageThird = GetSubImage(driver, imageBox);
                    //imageThird?.Save("c:/test2.png");
                }


                Console.WriteLine(pass ? "验证通过。" : "验证失败。");
                if (!pass)
                {
                    throw new Exception("极速验证码验证失败。");
                }
                //得到页面内容
                return(driver.PageSource);
            }
        }
Beispiel #3
0
 /// <summary>
 ///     CountDistance
 /// </summary>
 /// <param name="orgImage"></param>
 /// <param name="secondImage"></param>
 /// <returns></returns>
 public int CountDistance(Image orgImage, Image secondImage)
 {
     return(SlideImageHandler.FindXDiffRectangeOfTwoImage(orgImage, secondImage) - 7);
 }