Selenium automates full-screen screenshots, uses PIL to stitch scrolling screenshots

Selenium's default screenshot save_screenshot only supports screenshots of the current window content. If you want to capture the entire webpage, then you can tell you clearly.
Selenium can't do that.
You can manually use the developer tool Ctrl+Shift+P to bring up the command line menu, and execute the Capture full screenshot command to take a screenshot, as shown in the figure below:

You can also
call the method provided by Chrome DevTools Protocol through execute_cdp_cmd() of Selenium Webdriver. Unfortunately, however, there is no way to take a full-screen screenshot.

When using Selenium2, you can also use Firefox to take full-screen screenshots. After Selenium3, all browsers no longer support it.

One way is to use aShot , a jar package, which is obviously a Java-style solution.

In addition, if you don't have obsessive-compulsive disorder for styles, you can capture multiple +PIL pictures by scrolling the page to achieve full-screen screenshots.

Sample code:

from time import sleep
from PIL import Image
import numpy as np
from selenium import webdriver

driver = webdriver.Chrome()
driver.fullscreen_window()  # 全屏窗口
driver.get('https://www.qq.com/')
window_height = driver.get_window_size()['height']  # 窗口高度

page_height = driver.execute_script('return document.documentElement.scrollHeight')  # 页面高度
driver.save_screenshot('qq.png')

if page_height > window_height:
    n = page_height // window_height  # 需要滚动的次数
    base_mat = np.atleast_2d(Image.open('qq.png'))  # 打开截图并转为二维矩阵

    for i in range(n):
        driver.execute_script(f'document.documentElement.scrollTop={window_height*(i+1)};')
        sleep(.5)
        driver.save_screenshot(f'qq_{i}.png')  # 保存截图
        mat = np.atleast_2d(Image.open(f'qq_{i}.png'))  # 打开截图并转为二维矩阵
        base_mat = np.append(base_mat, mat, axis=0)  # 拼接图片的二维矩阵
    Image.fromarray(base_mat).save('hao123.png')

driver.quit()

PIL and numpy need to be installed: pip install PIL numpy

In the above example, the full screen window is used to obtain the maximum display range, the screen height is obtained through get_window_size(), and the page height is obtained by executing js.
Get the number of scrolls after division.
After each scroll, save the screenshot, and then use Image to open it and convert it into a two-dimensional matrix and stitch it into the two-dimensional matrix of the previous picture.
After the loop, finally output the spliced ​​two-dimensional matrix into a picture.

The effect is displayed, as shown in the figure below:​​​​​​​​

Note: If it is a stream-loaded page, the height of the page is constantly growing, and it is not fixed to the value of page_height obtained for the first time, which requires additional processing

Practical case

Optical theory is useless, you have to learn to follow along, and you have to do it yourself, so that you can apply what you have learned to practice. At this time, you can learn from some actual combat cases.

If it is helpful to you, please like and collect it to give the author an encouragement. It is also convenient for you to quickly find it next time.

If you don’t understand, please consult the small card below. The blogger also hopes to learn and progress with like-minded testers

At the right age, choose the right position, and try to give full play to your own advantages.

My road of automated test development is inseparable from the plan of each stage along the way, because I like planning and summarizing,

Test and develop video tutorials, study notes and receive portals! ! !

Guess you like

Origin blog.csdn.net/m0_59868866/article/details/130251669