使用位置\u一次\u将\u滚动到\u视图并要居中

2024-04-19 20:14:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用location\u once\u滚动到\u视图,滚动到网页上的特定元素。在我找到这些元素后,我正在拍摄它们的截图。我遇到的问题是,页面最终会向上滚动,并将元素放在导航栏下面。一旦它采取了截图,它只是一个导航栏的图片。是否有一种方法可以在将位置滚动到视图时继续使用位置,但不能一直滚动到页面顶部。而不是放在页面的中间

以下是我的屏幕截图功能的代码:

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 

Tags: image视图元素sizepngdriverlocation页面