python 截取屏幕_如何在python中使用Selenium WebDriver截取部分屏幕截图?

除了硒以外,此示例还需要PIL映像库。有时这是作为标准库之一放入的,有时则不是,但是如果没有它,则可以使用pip install Pillow

from selenium import webdriver

from PIL import Image

from io import BytesIO

fox = webdriver.Firefox()

fox.get('http://stackoverflow.com/')

# now that we have the preliminary stuff out of the way time to get that image :D

element = fox.find_element_by_id('hlogo') # find part of the page you want image of

location = element.location

size = element.size

png = fox.get_screenshot_as_png() # saves screenshot of entire page

fox.quit()

im = Image.open(BytesIO(png)) # uses PIL library to open image in memory

left = location['x']

top = location['y']

right = location['x'] + size['width']

bottom = location['y'] + size['height']

im = im.crop((left, top, right, bottom)) # defines crop points

im.save('screenshot.png') # saves new cropped image

最后输出是... Stackoverflow徽标!!!

当然,现在仅获取静态图像将是过大的选择,但是如果您想要获取需要Javascript才能实现的功能,那可能是一个可行的解决方案。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值