Can we Zoom the browser window in python selenium webdriver? Can we Zoom the browser window in python selenium webdriver? python python

Can we Zoom the browser window in python selenium webdriver?


I was just struggling with this. I managed to find something that works for me, hopefully it works for you:

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

Have 'zoom%' = whatever zoom level you want. (e.g. '67%')


Environment:

  • Selenium 3.6.0
  • chromedriver 2.33
  • Chrome version 62.0.3202.75 (Official Build) (64-bit)
  • macOS Sierra 10.12.6

I tried the ways (without use the CSS) that people suggested in other questions in the past. For example, the answers in this question: Selenium webdriver zoom in/out page content.

Or this: Test zoom levels of page on browsers

without success.

So, I thought: if not with the shortcuts, what could be a different way to do that?

The idea is to use the "chrome://settings/" page in order to change the zoom:

enter image description here

Ok I know, for example from Going through Chrome://settings by Selenium, that every settings should be set in the ChromeOptions.

From this question I noticed that in the list of preferences the only paramater (I think) could be:

// Double that indicates the default zoom level.const char kPartitionDefaultZoomLevel[] = "partition.default_zoom_level";

I tried, without success.

I want to repeat that I know it isn't the correct approach (and that will be different with different browser versions), but it works and, at least, was useful for me to understand how to go inside a shadow root element with selenium.

The following method return the elements inside a shadow root:

def expand_shadow_element(element):    shadow_root = driver.execute_script('return arguments[0].shadowRoot', element)    return shadow_root

It is useful because in the chrome://settings/ page there are shadow root elements.

In order to do that in my browser, this is the path:

root1=driver.find_element_by_xpath("*//settings-ui")shadow_root1 = expand_shadow_element(root1)container= shadow_root1.find_element_by_id("container")root2= container.find_element_by_css_selector("settings-main")shadow_root2 = expand_shadow_element(root2)root3=shadow_root2.find_element_by_css_selector("settings-basic-page")shadow_root3 = expand_shadow_element(root3)basic_page = shadow_root3.find_element_by_id("basicPage")

enter image description here

settings_section= basic_page.find_element_by_xpath(".//settings-section[@section='appearance']")root4= settings_section.find_element_by_css_selector("settings-appearance-page")shadow_root4=expand_shadow_element(root4)

enter image description here

and finally:

settings_animated_pages= shadow_root4.find_element_by_id("pages")neon_animatable=settings_animated_pages.find_element_by_css_selector("neon-animatable")zoomLevel= neon_animatable.find_element_by_xpath(".//select[@id='zoomLevel']/option[@value='0.5']")zoomLevel.click()

enter image description here

The entire code:

driver = webdriver.Chrome(executable_path=r'/pathTo/chromedriver')def expand_shadow_element(element):    shadow_root = driver.execute_script('return arguments[0].shadowRoot', element)    return shadow_rootdriver.get('chrome://settings/')root1=driver.find_element_by_xpath("*//settings-ui")shadow_root1 = expand_shadow_element(root1)container= shadow_root1.find_element_by_id("container")root2= container.find_element_by_css_selector("settings-main")shadow_root2 = expand_shadow_element(root2)root3=shadow_root2.find_element_by_css_selector("settings-basic-page")shadow_root3 = expand_shadow_element(root3)basic_page = shadow_root3.find_element_by_id("basicPage")settings_section= basic_page.find_element_by_xpath(".//settings-section[@section='appearance']")root4= settings_section.find_element_by_css_selector("settings-appearance-page")shadow_root4=expand_shadow_element(root4)settings_animated_pages= shadow_root4.find_element_by_id("pages")neon_animatable=settings_animated_pages.find_element_by_css_selector("neon-animatable")zoomLevel= neon_animatable.find_element_by_xpath(".//select[@id='zoomLevel']/option[@value='0.5']")zoomLevel.click()driver.get("https://www.google.co.uk/")

EDIT

As suggested by @Florent B in the comments, we can obtain the same result simple with:

driver.get('chrome://settings/')driver.execute_script('chrome.settingsPrivate.setDefaultZoom(1.5);')driver.get("https://www.google.co.uk/")

enter image description here


firefox solution for me,

Zoom body browser

zoom is a non-standard property, use transform instead (demo):

driver.execute_script("document.body.style.transform = 'scale(0.8)'")

https://github.com/SeleniumHQ/selenium/issues/4244

driver.execute_script('document.body.style.MozTransform = "scale(0.50)";')

driver.execute_script('document.body.style.MozTransformOrigin = "0 0";')