【发布时间】:2020-05-06 16:20:01
【问题描述】:
我正在使用 location_once_scrolled_into_view 滚动到网页上的特定元素。找到这些元素后,我正在对它们进行截图。我遇到的问题是页面最终向上滚动并将元素放在导航栏下方。截取屏幕截图后,它只是导航栏的图片。有没有办法继续使用 location_once_scrolled_into_view 但它不会一直滚动到页面顶部。而是到页面的中心?
这是我的截图功能的代码:
def take_screenshot(element, driver, filename):
location = element.location_once_scrolled_into_view
size = element.size
png = driver.get_screenshot_as_png() # saves screenshot of entire page
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
w , h = im.size
if w > 0 and h > 0:
im.save(filename)# saves new cropped image
【问题讨论】:
标签: python selenium beautifulsoup python-imaging-library lxml