Python uses Selenium to implement a screenshot instance _python

Source: Internet
Author: User
Tags in python

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.