Selenium+python Automation 82-the figure "reprint" of only one element is truncated

Source: Internet
Author: User
Tags truncated

Objective

Selenium intercept all the small partners know, once to interview, the interviewer asked: how an element of the diagram? Not all, as long as an element ... The small series of a sudden dumbfounded,
Painstaking people, the days are not negative, finally found a solution.

Selenium

1.selenium provides several ways to intercept a full screen

    • Get_screenshot_as_file (self, filename)

-This method obtains the current window, returns False when IOError occurs, and returns true successfully.
The filename parameter is the path to the saved file.

Driver.get_screenshot_as_file ('/screenshots/foo.png ')

    • Get_screenshot_as_base64 (self)

-This method is also to get the screen, save the base64 encoding format, when the HTML interface output, will be used.
For example, want to put in the HTML test report.

Driver.get_screenshot_as_base64 ()

    • Get_screenshot_as_png (self)

-This is to get the screen, save the binary data, rarely used.

Driver.get_screenshot_as_png ()

2.selenium actually provides a method for the element, but it will give an error. It is said that only the Edge browser is available, so you can give up.

Location get element coordinates

1. Take the Baidu Search button as an example, the location of the Print Search button:

# coding:utf-8from selenium import webdriverdriver = webdriver.Chrome()driver.get(‘http://www.baidu.com/‘)driver.save_screenshot(‘button.png‘)element = driver.find_element_by_id("su")print(element.location) # 打印元素坐标

2. Return result: {' Y ': 233.0, ' x ': 737.0}, as can be seen from the returned result, a dictionary type data is returned
x represents the horizontal axis and y represents the ordinate. (Everyone's computer window size is different, get the result is not the same, do not tangle)

Size gets the dimension of the element

1. Get the size of the element, you can get it with element.size.

= driver.find_element_by_id("su")print(element.size)                    # 打印元素大小

2. Return result: {' width ': +, ' height ': 36}, this also dictionary type, width is wide, height is high.

Installing Pillow

1.cmd Open, Input: Pip install Pillow

Case reference
# Coding:utf-8From seleniumImport WebdriverFrom PILImport Imagedriver= Webdriver. Chrome () Driver.get (' http://www.baidu.com/') driver.save_screenshot (' Button.png ') element= driver.find_element_by_id ("Su")Print (element.location)# print Element coordinatesPrint (element.size)# print element size left = element.location[< Span class= "hljs-string" > ' x ']top = element.location[ Y ']right = element.location[ ' x '] + Element.size[ ' width ']bottom = Element.location[ ' y '] + element.size[  ' height ']im = image. Open ( ' button.png ') im = im.crop (left, top, right, Bottom) Im.save ( ' button.png ')       

Selenium+python Automation 82-the figure "reprint" of only one element is truncated

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.