Example of using Selenium to implement web screenshot in Python

Source: Internet
Author: User

Selenium is a tool that allows the browser to automate a series of tasks, often used for automated testing. However, it can also be used to give Web pages. Currently, it supports four client languages for Java, C #, Ruby, and Python. If you use Python, you only need to enter "sudo easy_install selenium" on the command line and return to install Selenium's Python version of client support.

In Python, for example, we can use the following script to give specific pages (such as the home page of a script):

#-*-Coding:utf-8-*-## author:oldj <[email protected]># from selenium import webdriverimport time  def Captur E (URL, save_fn= "Capture.png"):  browser = webdriver. Firefox () # Get Local session of Firefox  Browser.set_window_size (browser.get) # (  URL) # Load page  Browser.execute_script ("" "    (function () {      var y = 0;      var step = +;      Window.Scroll (0, 0);       function f () {        if (Y < document.body.scrollHeight) {          y + = step;          Window.Scroll (0, y);          SetTimeout (F, a);        } else {          window.scroll (0, 0);          Document.title + = "Scroll-done";        }      }       SetTimeout (f, +);    }) ();  """)   For I in xrange (+):    if "Scroll-done" in Browser.title:      break    time.sleep (1)   browser.save_ Screenshot (SAVE_FN)  browser.close ()  if __name__ = = "__main__":   Capture ("Http://www.jb51.net")

  

Note that, in the above code, I did not immediately after opening the page, but first executed a JavaScript script on the page, first drag the scroll bar to the bottom of the page, and then drag it back to the top before. The advantage of this is that if there are some deferred loading content below the page, it will generally be loaded after this operation.

Compared to browser plug-ins such as Pagesaver, the Selenium function is more powerful, for example, it can inject and execute a JS on the page, but also can simulate the mouse clicks and other behaviors, and can run multiple instances simultaneously (multiple threads simultaneously). In this case, using Selenium to give the page seems like a good choice.

Using Selenium to implement a Web page instance in Python

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.