Using Selenium in Python to implement a web screenshot instance, pythonselenium

Source: Internet
Author: User

Using Selenium in Python to implement web page instances, pythonselenium

Selenium is a tool that allows the browser to automate a series of tasks and 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 Helper house ):

# -*- coding: utf-8 -*-## author: oldj <oldj.wu@gmail.com>#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.bkjia.com")

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.


In python, how does one use selenium to obtain the text displayed on the page?

I haven't tried your program yet, but I have encountered a similar situation. It is usually the dir element. Let's look at the methods and types. Because sometimes you think it is a text node, the content you actually want may still be in the subnode of this node.

"How to Use Python bindings for Selenium environment to open a link on a webpage"

Docs.seleniumhq.org/..xample

Driver. find_element_by_link_text ("AddToList"). click ()
 

Related Article

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.