人生襍多

파이썬 selenium 부분 캡처 본문

프로그래밍

파이썬 selenium 부분 캡처

이혁진 2019. 12. 9. 18:51

#selenium부분캡쳐 
#selenium필요한부분스크린샷 

selenium을 이용하여 화면을 캡쳐하는데,  
특정 위치만 캡처하고 싶을 때 아래와 같이 하면됩니다.  

 

구글링을 해봤는 적당한 예제가 없어서...


구글링을 하니 별 쓸때 없는 예제가 많아서 ... 
전체 소스는 아닙니다.  

element1 = browser.find_element_by_class_name('캡처할 위치의 메인 Html의 class명')  

element_png = element1.screenshot_as_png  
with open("test1.png", "wb") as file:  
    file.write(element_png) 

 

쓸때 없이, Image 패키지를 이용해서 

element = browser.find_element_by_class_name('캡처할 위치의 메인 Html의 class명')  
image_location = element.location  
image_size = element.size  

im = Image.open(BytesIO(png))  
left = image_location['x']  
top = image_location['y']  
right = image_location['x'] + image_size['width']  
bottom = image_location['y'] + image_size['height']  
im = im.crop((left, top, right, bottom))  
im.save('test2.png')  


이렇게 할 필요가 없습니다. 

​ 

이상입니다. 

Comments