Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we Zoom the browser window in python selenium webdriver?

I am trying to ZOOM IN and ZOOM OUT the Chrome( selenium webdriver) only using keyboard. I have tried --

from selenium.webdriver.common.keys import Keys
driver.find_element_by_tag_name("body").send_keys(Keys.CONTROL,Keys.SUBTRACT). 

but it is not working. Need answer in python.

like image 596
Dayananda Avatar asked Jan 23 '15 13:01

Dayananda


2 Answers

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%')

like image 50
Ben Avatar answered Sep 24 '22 01:09

Ben


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_root


driver.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

like image 30
Davide Patti Avatar answered Sep 22 '22 01:09

Davide Patti