如何使用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
相关产品推荐
相关产品推荐

