You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python Selenium将页面缩放至25%?

如何用Python Selenium将页面缩放至25%?

我需要在Python Selenium程序中将页面缩放至25%,使原本需滚动查看的所有元素都能直接显示。尝试了以下四种方法,但缩放效果均不符合预期:

  • 执行JavaScript修改页面body的zoom属性:
driver.execute_script("document.body.style.zoom='25%'")
  • 使用ActionChains模拟Ctrl+-按键操作:
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

actions = ActionChains(driver)
actions.key_down(Keys.CONTROL).send_keys(Keys.SUBTRACT).key_up(Keys.CONTROL).perform()
  • 循环发送Ctrl+-组合键来多次缩放:
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

zoomOut = ActionChains(driver)
zoomOut.key_down(Keys.CONTROL)
for i in range(7):
    print(i)
    zoomOut.send_keys("-")
zoomOut.key_up(Keys.CONTROL)
zoomOut.perform()
  • 参考的jQuery修改指定元素zoom属性的JavaScript代码:
driver.execute_script("$('id_body').css('zoom', 25);")

内容的提问来源于stack exchange,提问作者The_Fishy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 08:55:22