zoukankan      html  css  js  c++  java
  • 找到网页的某一区域,截图,并保存 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-windowsinphantomjs.exe") # ‪D:迅雷下载phantomjs-2.1.1-windowsinphantomjs.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:seleniumphantomjs-2.1.1-windowsin"))
                {
                    //进入百度首页
                    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文档

  • 相关阅读:
    【Java】_2_Java程序入门第五课
    【算法和数据结构】_3_线性结构_栈
    Windows程序设计_17_鼠标_1
    网络基础知识1:集线器,网桥,交换机
    [hyddd安全性测试笔记2]浅淡静态代码分析工具
    [Ruby小记]初试~
    Arp攻击实战
    [hyddd安全性测试笔记1]URL Encode and URL Decode
    网络嗅探技术浅析
    Session小记
  • 原文地址:https://www.cnblogs.com/baicaocanhua/p/6594172.html
Copyright © 2011-2022 走看看