Selenium is a tool that allows browsers to automate a series of tasks, often for automated testing. However, it can also be used to give screenshots to Web pages. Currently, it supports Java, C #, Ruby, and Python four client languages. If you use Python, you can install selenium's Python version of client support by typing "sudo easy_install selenium" on the command line and entering the carriage.
In Python, for example, we can use the following script to take a screenshot of a specified page (for example, the home of a cloud-dwelling community):
#-*-Coding:utf-8-*-# author:oldj <oldj.wu@gmail.com> # from Selenium import Webdriver Import time def capture (URL, save_fn= "Capture.png"): Browser = Webdriver. Firefox () # get local session of Firefox Browser.set_window_size (1200, 900) browser.get (URL) # Load page Browser.exe
Cute_script ("" "(function () {var y = 0;
var step = 100;
Window.Scroll (0, 0);
function f () {if (Y < document.body.scrollHeight) {y = = step;
Window.Scroll (0, y);
SetTimeout (F, 50);
else {window.scroll (0, 0);
Document.title + = "Scroll-done";
} settimeout (f, 1000);
})(); "" "For I in Xrange (): If" Scroll-done "in Browser.title:break time.sleep (1) Browser.save_screensh OT (SAVE_FN) browser.close () if __name__ = = "__main__": Capture ("Http://www.jb51.net")
Notice that in the code above, I didn't take a screenshot immediately after I opened the page, but instead I executed a JavaScript script on the page, dragging the page's scroll bar to the bottom, then dragging it back to the top and then taking a screenshot. The advantage of this is that if there is a bit of deferred loading at the bottom of the page, it is generally loaded after this operation.
Compared with pagesaver, such as browser Plug-ins, Selenium is more powerful, for example, it can inject and execute a section of JS on the page, but also simulate mouse clicks and other behaviors, but also can run multiple instances (multiple threads simultaneously screenshot). So it seems to be a good choice to use Selenium to give a screenshot of the page.