Using Selenium in Python to implement web page screenshot instances

Source: Internet
Author: User
This article describes how to use Selenium in Python to implement web pages. Selenium supports Java, C #, Ruby, Python, and other languages. This article uses the Python language as an example, if you need Selenium, Selenium is a tool that allows the browser to automate a series of tasks. it is often used for automated testing. However, it can also be used for webpage. Currently, it supports four client languages: Java, C #, Ruby, and Python. If you use Python, you only need to enter "sudo easy_install selenium" in the command line and press enter to install the Python client support of selenium.

Taking Python as an example, we can use the following script to specify a page (for example, the home page of the script family ):

# -*- coding: utf-8 -*-## author: oldj 
 
  #from selenium import webdriverimport timedef 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.execute_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(30):    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 open the page immediately, but first executed a JavaScript script on the page, first dragged the page's scroll bar to the bottom, and then dragged back to the top, then. The advantage is that if there is some delayed loading content at the bottom of the page, it is usually loaded after this operation.

Compared with browser plug-ins such as PageSaver, Selenium is more powerful. for example, it can inject and execute JavaScript code on the page and simulate mouse clicks and other behaviors, in addition, multiple instances can run simultaneously (multiple threads at the same time ). In this case, using Selenium for the page seems to be a good choice.

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.