Python+selenium screenshot picture and save the captured picture

Source: Internet
Author: User

This article reproduced: http://blog.csdn.net/u011541946/article/details/70141488

Http://www.cnblogs.com/timsheng/archive/2012/09/05/2672651.html

It is necessary to introduce how to use the Selenium method in the testing process, especially when encountering errors. There are three main methods in selenium for Python, and we pick one of the most common ones.

Skills should be a more important skill for testers.

In automated tests, you can help us visually locate errors and record test steps.

Remember that in the past when the automation project for a multinational bank, a bank of the PM requirements of our automated testing at least 1 each step to prove that each function has been automated testing to cover, in this case becomes a proof of the effectiveness of automated testing is an important means.

Good testers will intercept a good figure, with the Ashes level otaku will sing a good poem.

The Webdriver is very powerful. Before the time, the most troublesome problem is that the page is too long to intercept only one screen, outside the screen need to move the scroll bar to see the area is generally not truncated. Now Webdriver solves this problem, no matter how long the page, Webdriver can be relatively perfect cut to the complete page.

The following code demonstrates how to use Webdriver:

1 #-*-coding:utf-8-*-2  fromSeleniumImportWebdriver3 ImportUnitTest4 ImportOs,sys,time5 ImportHtmltestreport6 7 #Login8Driver =Webdriver. Firefox ()9 TenCurrent_time = Time.strftime ("%y-%m-%d-%h_%m_%s", Time.localtime (Time.time ())) OneCurrent_time1 = Time.strftime ("%y-%m-%d", Time.localtime (Time.time ())) A Print(current_time) - Print(current_time1) - #You must print the picture path Htmltestrunner to capture and generate the path, \image\**\\**.png is the condition that gets the path, the directory must be such the #set the storage picture path, the test result picture can be differentiated by daily -  -  -  + #assertion Judging by an if -Driver.get ("https://baidu.com/") + #New Create path "." Represents the location of the current entire. py file's path, the "\ \" path separator, where one is "\" for the escape character APic_path ='. \\result\\image\\'+ current_time1+'\\'+ Current_time +'. PNG' at Print(Pic_path) -Time.sleep (5) - Print(Driver.title) - #capture the picture of the current URL page and save the captured image under the specified path (pic_path), note: Both methods can - driver.save_screenshot (Pic_path) -Driver.save_screenshot ('. \\result\\image\\'+ current_time1+'\\'+ Current_time +'. PNG')   in  - ifU'Baidu a bit, you will know'==Driver.title: to     Print('assertion test Pass.')  + Else: -     Print('assertion test fail.') the  *  #assertion judgment by throwing an exception through a try $Driver.get ("https://baidu.com/")Panax Notoginseng driver.save_screenshot (Pic_path) - Try: the     assertU'Baidu a bit, you will know'==Driver.title +     Print('assertion test Pass.')   A exceptException as E: the     Print('assertion test fail.', Format (e)) +  -Time.sleep (5) $Driver.quit ()

The Save_screenshot method realizes the function, only needs to pass in the saved file name to be possible, is very convenient.

Python+selenium pictures and save the captured pictures

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.