2

I found this for java :-

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

But how to do this using Python? I want to zoom out one level after get requests.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Shekhar Samanta
  • 875
  • 2
  • 12
  • 25

6 Answers6

14

this does the job for Python with selenium (-v 3.141.0)

driver = webdriver.Chrome(executable_path='...')
driver.get("www.stackoverflow.com")
driver.execute_script("document.body.style.zoom='50%'")
iacomino
  • 149
  • 1
  • 2
10

You can do something similar where you specify the zoom levels as below:

For example, if my body has a <div id='values'>, I could zoom the div using

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()

Or Simply try:

driver.execute_script("document.body.style.zoom='zoom %'")
Vaulstein
  • 20,055
  • 8
  • 52
  • 73
  • selenium.common.exceptions.WebDriverException: Message: $ is not a function This is the page - driver.get('http://finviz.com/quote.ashx?t=A&ty=c&ta=1&p=d&b=1') I can't figure out how to zoom out – Shekhar Samanta Aug 21 '15 at 09:05
  • Thanks Vaulstein, this works for me - driver.execute_script("document.body.style.zoom='90%'") After setting the zoom value to 90% , i want to go bit down using keys.DOWN 4 times then screenshot . How can i do that ? – Shekhar Samanta Aug 21 '15 at 09:21
  • 2
    You can use `driver.execute_script("window.scrollTo(0, X)")' where `X` is the vertical position you want to scroll. – Vaulstein Aug 21 '15 at 09:25
  • Thanks Vaulstein, your help really saved my Day. Below i added - driver.save_screenshot('D:\capture.png') – Shekhar Samanta Aug 21 '15 at 10:03
7

I looked all over for a solution to zoom out using selenium too, the documentation mentions nothing. Luckily the driver for Firefox (geckodriver) have this on one of their Github issues

I've made a shorthand brief of how to zoom out (and in), that will hopefully make the most sense (or at least that did for me)

#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")
dope
  • 156
  • 2
  • 6
  • What does exactly mean `driver.set_context("chrome")` – Marcelo Gazzola Jun 02 '20 at 02:48
  • keep it chrome even for firefox – Basheer AL-MOMANI Jun 09 '20 at 22:20
  • @BasheerAL-MOMANI Yes, set the context to "chrome" even for firefox, I'm not exactly sure why. But it works :). If anyone happens to know why the word 'chrome' is used here, I'd be curious if anyone could shine some light on this for completeness. – dope Jul 25 '20 at 23:25
  • 1
    Thanks a lot, setting window as tag name it did not work for me, I set html as tag name and it worked perfectly. – badr Jun 21 '22 at 15:46
  • 1
    On Firefox 91.13.0ESR, GeckoDriver 0.31.0 on Linux, I had to replace "window" with "html". But, it causes unexpected behavior eg: even if textbox is filled, the value is not sent to API request. Issue is not encountered manually. – SmiP Sep 12 '22 at 12:53
  • 1
    regarding , driver.set_context("chrome") ... In Firefox browser, the chrome means any visible aspect of a browser aside from the webpages themselves(e.g., toolbars, menu bar, tabs). This is not to be confused with the Google Chrome browser. – vrooom Jun 28 '23 at 10:49
2

This works for 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')
brian.m
  • 21
  • 1
1

Hey guys I had issues with all solutions here using firefox, some solutions didn´t work exactly as expected so with all your help I create this one.

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")
Efeuncode
  • 11
  • 1
0

I tried to reproduce best suggestions here and got the error

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

I found work around, I need to zoom out for 75%, for that purpose I use pyautogui

import pyautodui
....

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