【问题标题】:How to zoom out of page using python selenium如何使用 python selenium 缩小页面
【发布时间】:2021-04-25 09:39:22
【问题描述】:

我为 java 找到了这个:-

WebElement html = driver.findElement(By.tagName("html"));
html.sendKeys(Keys.chord(Keys.CONTROL, Keys.ADD));

但是如何使用 Python 做到这一点?我想在获取请求后缩小一级。

【问题讨论】:

    标签: python selenium selenium-webdriver


    【解决方案1】:

    您可以在指定缩放级别时执行类似的操作,如下所示:

    例如,如果我的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: Message: $ is not a function 这是页面 - driver.get('finviz.com/…) 我不知道如何缩小
    • 感谢 Vaulstein,这对我有用 - driver.execute_script("document.body.style.zoom='90%'") 将缩放值设置为 90% 后,我想向下一点使用keys.DOWN 4次然后截图。我该怎么做?
    • 你可以用driver.execute_script("window.scrollTo(0, X)")' where X`是你要滚动的垂直位置。
    • 感谢 Vaulstein,您的帮助真的挽救了我的一天。下面我添加了 - driver.save_screenshot('D:\capture.png')
    【解决方案2】:

    这可以使用 selenium (-v 3.141.0) 完成 Python 的工作

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

    【讨论】:

      【解决方案3】:

      我也到处寻找使用硒进行缩小的解决方案,文档没有提及。幸运的是,Firefox 的驱动程序(geckodriver)在他们的 Github 之一上有这个 issues

      我已经简要介绍了如何缩小(和缩小),希望这将是最有意义的(或者至少对我来说是这样)

      #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")是什么意思
      • 即使在 Firefox 上也能保持 chrome
      • @BasheerAL-MOMANI 是的,即使对于 Firefox,也将上下文设置为“chrome”,我不确定为什么。但它有效:)。如果有人碰巧知道为什么在这里使用“chrome”这个词,我会很好奇是否有人可以为完整性提供一些启示。
      【解决方案4】:

      这适用于 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')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-02-08
        • 2020-03-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-01
        相关资源
        最近更新 更多