首页 > 代码库 > 找到网页的某一区域,截图,并保存
找到网页的某一区域,截图,并保存
python 实现
from PIL import Imagefrom selenium import webdriverimport 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文档
找到网页的某一区域,截图,并保存
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。