C# PhantomJS+Selenium 自动化快速截图+区域截图

 static void Main(string[] args)
        {
            var url = "http://192.168.20.58/gfwd/comments-1.html";
            using (var driver = new PhantomJSDriver(@"E:\selenium\phantomjs-2.1.1-windows\bin\"))
            {
                //进入百度首页
                driver.Navigate().GoToUrl(url);
                driver.Manage().Window.Size = new Size(1280, 800);

                //设置窗体最大化
                //driver.Manage().Window.Maximize();
                //找到对象
                var imgelement = driver.FindElementById("screen");
                var location = imgelement.Location;
                var size = imgelement.Size;

                var savepath = Environment.CurrentDirectory + "\\codingpy_1.png";
                driver.GetScreenshot().SaveAsFile(savepath, ScreenshotImageFormat.Png);//屏幕截图   
                Image image = System.Drawing.Image.FromFile(savepath);
                int left = location.X;
                int top = location.Y;
                int right = left + size.Width;
                int bottom = top + size.Height;
                //截图
                Bitmap bitmap = new Bitmap(savepath);//原图
                Bitmap destBitmap = new Bitmap(size.Width, size.Height);//目标图
                Rectangle destRect = new Rectangle(0, 0, size.Width, size.Height);//矩形容器
                Rectangle srcRect = new Rectangle(left, top, size.Width, size.Height);
                Graphics graphics = Graphics.FromImage(destBitmap);
                graphics.DrawImage(bitmap, destRect, srcRect, GraphicsUnit.Pixel);
                destBitmap.Save("d:\\aa.Png");
                graphics.Dispose();
            }
        }

 

你可能感兴趣的:(C# PhantomJS+Selenium 自动化快速截图+区域截图)