Selenium+python automation 82 - only cut a picture of an element

foreword

Selenium screenshots the full picture Everyone knows that when I went to the interview, the interviewer asked: How to take a screenshot of a certain element? Not all, just a certain element. . . The editor was dumbfounded all of a sudden, and the
painstaking effort, the gods live up to it, finally found a solution.

selenium screenshot

1. Selenium provides several methods to capture the full screen

  • get_screenshot_as_file(self, filename)

--This method is to get a screenshot of the current window, return False when an IOError occurs, and return True when the screenshot is successful.
The filename parameter is the path to save the file.

driver.get_screenshot_as_file('/Screenshots/foo.png')

  • get_screenshot_as_base64(self)

--This method is also to obtain screenshots, which are stored in the base64 encoding format, which will be used when outputting screenshots in the HTML interface.
For example, want to put screenshots in the html test report.

driver.get_screenshot_as_base64()

  • get_screenshot_as_png(self)

--This is to get a screenshot, which saves binary data, which is rarely used.

driver.get_screenshot_as_png()

2. Selenium actually provides a method for taking screenshots of elements, but it will report an error. It is said that only the Edge browser can be used, so you can give up.

location gets element coordinates

1. Take Baidu's search button as an example, print the location of the search button:

# coding:utf-8
from selenium import webdriver driver = 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 the result: {'y': 233.0, 'x': 737.0}, as can be seen from the returned result, what is returned is a dictionary type data
x represents the abscissa, y represents the ordinate. (Everyone's computer window The size is different, the result is different, don't worry)

size gets the element size

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

element = driver.find_element_by_id("su")

print(element.size)                    # 打印元素大小

2. Return result: {'width': 100, 'height': 36}, this is also a dictionary type, width is width, height is height.

install pillow

1. Open cmd and enter: pip install pillow

Case reference

# coding:utf-8
from selenium import webdriver
from PIL import Image
driver = 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 coordinates 
print (element.size)                     #Print element size 

left = element.location[ ' 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')

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325324333&siteId=291194637