首页 > 解决方案 > 使用 selenium python 截取整个页面截图

问题描述

我正在尝试使用 selenium 和 chromedriver 截取整页截图,但我得到了这个半截截图。我尝试过其他方法,但到目前为止只有这一种有效,而且只需要半页截图。

任何人都可以使用另一个技巧来解决这个问题,也可以附加输出结果。

页面截图

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options


options = webdriver.ChromeOptions()
options.headless = True
driver = webdriver.Chrome(options=options)

URL = "http://stevens.ekkel.ai"

driver.get(URL)

S = lambda X: driver.execute_script('return document.body.parentNode.scroll'+X)
driver.set_window_size(S('Width'),S('Height')) # May need manual adjustment                                                                                                                
driver.find_element_by_tag_name('body').screenshot('web_screenshot.png')

driver.quit()

标签: pythonpython-3.xseleniumselenium-webdriverselenium-chromedriver

解决方案


我正在使用 PyPI 包:Selenium-Screenshot
您可以使用命令安装它 -pip install Selenium-Screenshot
可以在此处找到此包的详细信息:https ://pypi.org/project/Selenium-Screenshot/

分享一个关于如何使用这个包进行整页截图的示例补丁:

from Screenshot import Screenshot_Clipping
        
#Saving screenshot
ob=Screenshot_Clipping.Screenshot()
img=ob.full_Screenshot(driver,save_path=r'D:/OneDrive -Libin/Python/Sel_python/Pytest/Screenshots',image_name="Screenshot1.png")
    
driver.close()
driver.quit()

检查屏幕截图以供参考:https ://i.stack.imgur.com/opgPi.png


推荐阅读