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.