Search code examples
pythonseleniumzooming

Python: How to zoom out of a page using selenium?


I am trying to zoom out to 25% in my python selenium program,

It should zoom out from this: enter image description here

to this: enter image description here

As you can see the elements that should appear when scrolling down in the first image are all visible in the second image when zoomed out to 25%.

I tried driver.execute_script("document.body.style.zoom='25%'") but that's how it zoomed out: enter image description here

For some reason these solutions didn't do anything for me:

1-

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

actions = ActionChains(driver)
actions.key_down(Keys.CONTROL).send_keys(Keys.SUBTRACT).key_up(Keys.CONTROL).perform()

2-

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

zoomOut = ActionChains(driver)
zoomOut.key_down(Keys.CONTROL)
for i in range(7):
    print(i)
    zoomOut.send_keys("-")
zoomOut.key_up(Keys.CONTROL)
zoomOut.perform()

3- The solution driver.execute_script("$('id_body').css('zoom', 25);") in this question doesn't do anything in my program.


Solution

  • So the answer is that the problem is from the website, to be able to see all the elements we should scroll down so that the rest of the data is loaded.