Python selenium zoom out

To perform zoom out in Selenium using Python, you can make use of the `execute_script` method in Selenium WebDriver. This method allows executing JavaScript code within the browser context, which enables manipulating the zoom level of the page.

Here’s an example:


from selenium import webdriver

# Instantiate the Chrome browser
driver = webdriver.Chrome()

# Open a webpage
driver.get("https://www.example.com")

# Zoom out by 50%
driver.execute_script("document.body.style.zoom = '50%'")

In the above example, we first import the `webdriver` module and instantiate the Chrome browser. We then open a webpage using the `get` method and pass the URL of the webpage as an argument.

Next, we use the `execute_script` method to execute JavaScript code that sets the `zoom` property of `body` to “50%”. This effectively zooms out the page by 50%.

You can adjust the zoom level by changing the value in the JavaScript code. For example, to zoom out by 75%, you would use:


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

By executing JavaScript using `execute_script`, you have the flexibility to customize the zoom level according to your needs.

Leave a comment