Skip to main content
 首页 » 编程设计

python之如何确保在 headless 模式 Firefox 驱动程序中截取的屏幕截图尺寸始终相同

2024年12月31日12zhenyulu

我想全屏截取本地 html 页面的屏幕截图。但是以下代码为不同的 html 文件生成不同的维度文件(屏幕截图)。我觉得这很奇怪,因为我正在全屏显示,这不应该发生。请帮忙。

firefox_options = Options() 
firefox_options.add_argument("--headless") 
driver = webdriver.Firefox(firefox_options=firefox_options) 
driver.get('file:///'+q.html.path) 
driver.fullscreen_window() 
shot = driver.get_screenshot_as_png() 
driver.close() 

请您参考如下方法:

只需在headless后面添加一个选项来设置窗口大小:

firefox_options.add_argument("--window-size=1920x1080") 

headless 模式不需要 driver.fullscreen_window() 行。