Ejemplo n.º 1
0
def _full_screenshot_old(browser: webdriver, offset: int = 0) -> Image:
    """ Получение полного скриншота содержимого окна браузера
        offset - начальное смещение"""

    img_list = []  # для хранения фрагментов изображения

    # Определение размера окна браузера
    height = browser.execute_script("""return Math.max(
                                           document.documentElement.clientHeight,
                                           window.innerHeight
                                       );""")

    # Определение высоты содержимого окна браузера
    max_window_height = browser.execute_script("""return Math.max(
                                                      document.body.scrollHeight,
                                                      document.body.offsetHeight,
                                                      document.documentElement.clientHeight,
                                                      document.documentElement.scrollHeight,
                                                      document.documentElement.offsetHeight
                                                  );""")

    # Скроллим страницу и добавляем скриншот видимого окна браузера в img_list
    while offset < max_window_height:
        time.sleep(1)
        browser.execute_script(f"window.scrollTo(0, {offset});")

        img = Image.open(BytesIO((browser.get_screenshot_as_png())))
        img_list.append(img)

        offset += height

    # Обрезание последнего изображения
    box = (0, height - height *
           (max_window_height / height - max_window_height // height),
           img_list[-1].size[0], img_list[-1].size[1])
    img_list[-1] = img_list[-1].crop(box)

    # Определение размеров нового изображения, создание холста
    img_frame_height = sum([img_frag.size[1] for img_frag in img_list])
    img_frame = Image.new('RGB', (img_list[0].size[0], img_frame_height))

    # Объединение изображений в одно
    offset = 0
    for img_fragment in img_list:
        img_frame.paste(img_fragment, (0, offset))
        offset += img_fragment.size[1]

    return img_frame
Ejemplo n.º 2
0
def get_statistics_screenshot(driver: webdriver) -> Image.Image:
    # Ресайз окна, чтобы поместилась вся статистика
    driver.set_window_size(1920, 600)

    # Область со статистикой
    summary = driver.find_element_by_class_name('summary--emotion')
    statistics_location = summary.location_once_scrolled_into_view
    statistics_size = summary.size

    # Скриншот всего окна
    statistics = Image.open(BytesIO(driver.get_screenshot_as_png()))

    # Получаем ROI
    left = int(statistics_location['x'])
    top = int(statistics_location['y'])
    right = left + int(statistics_size['width'])
    bottom = top + int(statistics_size['height'])
    roi = (left, top, right, bottom)
    return statistics.crop(roi)
Ejemplo n.º 3
0
    def fetch(self, driver: webdriver) -> Image:
        device_pixel_ratio = driver.execute_script(
            'return window.devicePixelRatio')
        total_height = driver.execute_script(
            'return document.body.parentNode.scrollHeight')
        viewport_height = driver.execute_script('return window.innerHeight')
        total_width = driver.execute_script('return document.body.offsetWidth')
        viewport_width = driver.execute_script(
            "return document.body.clientWidth")

        try:
            assert (viewport_width == total_width)
        except AssertionError as e:
            print('screen: ', e)

        # scroll the page, take screenshots and save screenshots to slices
        offset = 0
        slices = {}
        while offset < total_height:
            if offset + viewport_height > total_height:
                offset = total_height - viewport_height

            driver.execute_script('window.scrollTo({0}, {1})'.format(
                0, offset))
            time.sleep(self.scroll_delay)

            img = Image.open(BytesIO(driver.get_screenshot_as_png()))
            slices[offset] = img

            offset = offset + viewport_height

        # combine image slices
        stitched_image = Image.new('RGB', (total_width * device_pixel_ratio,
                                           total_height * device_pixel_ratio))
        for offset, image in slices.items():
            stitched_image.paste(image, (0, offset * device_pixel_ratio))

        return stitched_image