Selenium的PrintOptions.page_height为何在部分网站中失效?
Selenium PrintOptions.page_height 在特定网站完全失效的问题
问题背景
Selenium 提供的 print_page 方法可返回页面加载内容的Base64编码PDF文件,该方法接收 PrintOptions 对象,用于配置PDF渲染的各类参数(如页面高度/宽度、边距等)。
多数测试网站中,PrintOptions.page_height 属性均可正常工作,示例代码如下:
from selenium import webdriver import base64 from selenium.webdriver.common.print_page_options import PrintOptions selenium_driver = webdriver.Firefox() selenium_driver.get("https://www.google.com/") print_options = PrintOptions() print_options.page_height = 10 # 设置PDF页面高度 base64_encoded = selenium_driver.print_page(print_options) with open("print.pdf", 'wb') as file: file.write(base64.b64decode(base64_encoded))
异常现象
在部分特定网站(如Roundcube演示站点的邮件打印友好版页面)中,page_height 属性完全被忽略,PDF会按其他尺寸(如A3)生成。
环境信息
- Selenium 版本:v4.25.0
- Geckodriver 版本:v0.35.0
补充说明
此问题不同于Bugzilla编号1833964的问题,本问题中page_height是完全失效,而非数值不精确。
内容的提问来源于stack exchange,提问作者wiki
相关产品推荐
相关产品推荐

