如何用python得到手机端页面的完整截图?

我想要用python获取一个手机端页面完整的长截图。我用了mobile emulation来设置长和宽。这个页面太长了,我hardcode去获取了固定的数值。

现在设定的宽是375,长是22000,但如果是这么设定的话,我会得到以下这个报错"Time out receiving a message from the renderer"。

现在有两个问题让我感到困惑:

  1. 我是把固定的长和宽的数值放在mobile emulation里的,且长的值特别大,这也做的对的吗?
  2. driver.set_window_size(width, height-1800) 我不确定这一行代码是否需要改成driver.set_window_size(width, height)或者别的?

下面是比较完整的代码,如果有什么问题的话,请随时告诉我,先谢啦!

def save_screenshot(url):
         mobile_emulation = {
            "deviceMetrics": { "width": 375, "height": 22000, "pixelRatio": 3.0 },
            "userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) "
                         "AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile 
                          Safari/535.19" }
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
        chrome_options.add_argument("start-maximized")
        chrome_options.add_argument("enable-automation")
        chrome_options.add_argument("--headless")
        chrome_options.add_argument("--no-sandbox")
        chrome_options.add_argument("--disable-infobars")
        chrome_options.add_argument("--disable-dev-shm-usage")
        chrome_options.add_argument("--disable-browser-side-navigation")
        chrome_options.add_argument("--disable-gpu") 
        driver = webdriver.Chrome(chrome_options=chrome_options) 
        driver.get(url)

        driver.implicitly_wait(5)

        width = driver.execute_script("return document.documentElement.scrollWidth")
        height = driver.execute_script("return document.documentElement.scrollHeight")
        print(width,height)

        driver.set_window_size(width, height-1800)
        time.sleep(1)

        driver.save_screenshot(NAME)

参考一下:https://blog.csdn.net/miaomiao_zhang/article/details/86237195
亲测可用
如果对你有帮助,可以点击我这个回答右上方的【采纳】按钮,给我个采纳吗,谢谢