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文档