找到网页的某一区域,截图,并保存 HTML to Image

python 实现

http://www.jianshu.com/p/7ed519854be7 

利用 Python + Selenium 实现对页面的指定元素截图(可截长图元素)

http://codingpy.com/article/take-screenshot-of-web-page-using-selenium/

利用 Python + Selenium 自动化快速截图


from
PIL import Image from selenium import webdriver import time; # 引入time模块 def webscreen(i): print(i) if i == 1 or i == 700: print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) # url = 'http://192.168.20.58/gfwd/comments-1.html' url = 'http://192.168.20.58/gfwd/comments-' + str(i) + '.html' # driver = webdriver.Chrome("C:/Users/maimai/Downloads/chromedriver_win32/chromedriver.exe") # driver = webdriver.PhantomJS("‪D:/迅雷下载/phantomjs-2.1.1-windows/bin/phantomjs.exe") driver = webdriver.PhantomJS(r"D:\迅雷下载\phantomjs-2.1.1-windows\bin\phantomjs.exe") # ‪D:\迅雷下载\phantomjs-2.1.1-windows\bin\phantomjs.exe driver.set_page_load_timeout(300) driver.set_window_size(1280, 800) driver.get(url) imgelement = driver.find_element_by_id('screen') location = imgelement.location size = imgelement.size savepath = r'images/codingpy_' + str(i) + '.png' driver.save_screenshot(savepath) im = Image.open(savepath) left = location['x'] top = location['y'] right = left + size['width'] bottom = location['y'] + size['height'] im = im.crop((left, top, right, bottom)) im.save(savepath)

 c#实现

 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#对图片文件的压缩、裁剪操作初探 http://www.cnblogs.com/xyang/archive/2013/02/25/2932145.html

            

自动化测试:Selenium webdriver 学习笔记-C#版(二)

http://seleniumhq.github.io/selenium/docs/api/dotnet/index.html api文档

转载于:https://www.cnblogs.com/baicaocanhua/p/6594172.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值