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

Time:12-27

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.

CodePudding user response:

I tested the first option (second option same as this) and found that the code works great. But it works only at the page elements level not at the window level or chrome itself. You can see for yourself by running the code below.

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
# set chromodriver.exe path
driver = webdriver.Chrome(executable_path='C:\\chromedriver\\chromedriver108.exe')
# object of ActionChains
action = ActionChains(driver)
# launch URL
# driver.get("https://keyboard-test.space/")
driver.get("https://en.key-test.ru/")
# driver.get("https://keyboardtestt.com/")
action.key_down(Keys.CONTROL).send_keys(Keys.SUBTRACT).key_up(Keys.CONTROL).perform()
# wait user input
input()
# close browser
driver.close()

Then I browsed stackoverflow on related topics. In the Why are Selenium Chromedriver shortcuts not working? @pcalkins says:

The scope is limited to the DOM more or less. There used to be some support for it (not necessarily by design), but I think all the webdrivers/browsers dropped it.

In the python selenium sendkey doesn't work in chrome @soundwave says:

Webdriver is designed to be used to drive the web-page, rather than to do browser specific actions. See here bugs.chromium.org

"This is a limitation in the way we simulate keyboard input in ChromeDriver. Keys get sent directly to the render process, bypassing the browser process. So any keyboard shortcut handlers in the browser process will not be invoked"

and in the "Selenium Chrome Driver send key press combinations to window" @AndrewRegan says:

The WebDriver spec is element-focussed, and doesn't define any method to send keys to the window, the screen, to browser chrome - only to elements.

Use of the Selenium Actions class for Cmd-R works on my Mac in Firefox (45), but only when run in the foreground - and seemingly not at all in Chrome. Presumably this is down to differences in the implementations of the remote Keyboard implementation, which it's probably best not to rely upon.

CodePudding user response:

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.

  • Related