找到網頁的某一區域,截圖,並保存 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文檔


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM