使用 location_once_scrolled_into_view 并希望居中
Using location_once_scrolled_into_view and want to center
我正在使用 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
您可以使用 JavaScript scrollIntoView
和 options:
driver.execute_script('arguments[0].scrollIntoView({block: "center", inline: "center"})', element)
我正在使用 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
您可以使用 JavaScript scrollIntoView
和 options:
driver.execute_script('arguments[0].scrollIntoView({block: "center", inline: "center"})', element)