如何使用Python Selenium缩小页面

4
我找到了这个关于Java的内容:-
WebElement html = driver.findElement(By.tagName("html"));
html.sendKeys(Keys.chord(Keys.CONTROL, Keys.ADD));

但是如何使用Python实现这一操作呢?我想在进行GET请求后缩小一个级别。

6个回答

15

这个可以在Python中使用selenium(版本号为-v 3.141.0)来完成。

driver = webdriver.Chrome(executable_path='...')
driver.get("www.stackoverflow.com")
driver.execute_script("document.body.style.zoom='50%'")

10

您可以采用以下方式指定缩放级别来实现类似的效果:

例如,如果我的body有一个<div id='values'>,我可以使用以下代码对该div进行缩放:

from selenium import webdriver

def main():
    browser = webdriver.Chrome()
    browser.set_window_size(1000, 1000)
    browser.get("http://yoursite.com")
    browser.execute_script("$('#values').css('zoom', 5);")

if __name__ == '__main__':
    main()

或者简单尝试:

driver.execute_script("document.body.style.zoom='zoom %'")

selenium.common.exceptions.WebDriverException: 消息:$ 不是一个函数这是页面 -driver.get('http://finviz.com/quote.ashx?t=A&ty=c&ta=1&p=d&b=1')我无法弄清楚如何缩小页面 - undefined
谢谢Vaulstein,这对我有用 - driver.execute_script("document.body.style.zoom='90%'")在将缩放值设置为90%之后,我想使用keys.DOWN按键向下移动4次,然后截屏。我该如何做到这一点? - undefined
2
你可以使用driver.execute_script("window.scrollTo(0, X)"),其中的X是你想要滚动的垂直位置。 - undefined
谢谢Vaulstein,你的帮助真的救了我的一天。 下面我添加了 - driver.save_screenshot('D:\capture.png') - undefined

8

我曾经搜寻过一种使用selenium缩小页面的解决方案,但文档中并没有提到。幸运的是,Firefox(geckodriver)的驱动程序在他们的Github问题列表中提到了这个功能。

以下是如何缩小(或放大)页面的简短指南,希望对您有所帮助。

#I'm sure this will be interchangeable with the Chrome driver too
 driver = webdriver.Firefox()
#Set the focus to the browser rather than the web content
 driver.set_context("chrome")
#Create a var of the window
 win = driver.find_element_by_tag_name("window")
#Send the key combination to the window itself rather than the web content to zoom out
#(change the "-" to "+" if you want to zoom in)
 win.send_keys(Keys.CONTROL + "-")
#Set the focus back to content to re-engage with page elements
 driver.set_context("content")

driver.set_context("chrome") 这句代码的确切含义是什么? - undefined
保持它在Chrome上的外观,即使在Firefox上也是如此。 - undefined
@BasheerAL-MOMANI 是的,即使对于火狐浏览器,将上下文设置为“chrome”。我不确定为什么会这样,但它确实有效:)。如果有人知道为什么在这里使用了“chrome”一词,我很好奇是否有人能够完整地解释一下。 - undefined
1
非常感谢,将窗口设置为标签名称对我没有起作用,我将HTML设置为标签名称,它完美地工作了。 - undefined
1
在Firefox 91.13.0ESR,Linux上的GeckoDriver 0.31.0中,我不得不用“html”替换“window”。但是,这会导致意外的行为,例如:即使文本框已填充,值也不会发送到API请求。手动操作时未遇到问题。 - undefined
1
关于 driver.set_context("chrome") ... 在 Firefox 浏览器中,"chrome" 指的是除了网页本身之外浏览器的任何可见部分(例如工具栏、菜单栏、选项卡)。这与 Google Chrome 浏览器不同,请勿混淆。 - undefined

3

大家好,我在使用火狐浏览器时遇到了一些问题,有些解决方案并没有像预期的那样完全奏效,所以在大家的帮助下,我创建了这个解决方案。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
    
driver.implicitly_wait(5)

driver.get("https://stackoverflow.com/")
#Set the focus to the browser rather than the web content
driver.set_context("chrome")
#Create a var of the window
win = driver.find_element(By.TAG_NAME,"html")
#for zoom out this will change to 90% if you need more copy and paste it again. Or you can change - to + for zoom in.    
win.send_keys(Keys.CONTROL + "-")
    
#Set the focus back to content to re-engage with page elements
driver.set_context("content")

2

这适用于IE浏览器

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Ie(executable_path=path_to_driver, capabilities={'ignoreZoomSetting':True})
driver.get(url)
driver.find_element_by_tag_name('html').send_keys(Keys.CONTROL, '0')

0

我尝试在这里复制最佳建议,但出现了错误

driver.set_context("chrome")
AttributeError: 'WebDriver' object has no attribute 'set_context'

我找到了一个解决办法,我需要将页面缩小到75%,为此我使用了pyautogui

import pyautodui
....

    pyautogui.keyDown('ctrl')
    j: int = 1
    for j in range(3):
        pyautogui.press('-')
        time.sleep(1)
    pyautogui.keyUp('ctrl')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接