TechBeamersTechBeamers
  • Viral Tips 🔥
  • Free CoursesTOP
  • TutorialsNEW
    • Python Tutorial
    • Python Examples
    • C Programming
    • Java Programming
    • MySQL Tutorial
    • Selenium Tutorial
    • Selenium Python
    • Playwright Python
    • Software Testing Tutorial
    • Agile Concepts
    • Linux Concepts
    • HowTo Guides
    • Android Topics
    • AngularJS Guides
    • Learn Automation
    • Technology Guides
  • Top Interviews & Quizzes
    • SQL Interview Questions
    • Testing Interview Questions
    • Python Interview Questions
    • Selenium Interview Questions
    • C Sharp Interview Questions
    • Java Interview Questions
    • Web Development Questions
    • PHP Interview Questions
    • Python Quizzes
    • Java Quizzes
    • Selenium Quizzes
    • Testing Quizzes
    • HTML CSS Quiz
    • Shell Script Quizzes
  • ToolsHOT
    • Python Online Compiler
    • Python Code Checker
    • C Online Compiler
    • Review Best IDEs
    • Random Letter Gen
    • Random Num Gen
TechBeamersTechBeamers
Search
  • Viral Tips 🔥
  • Free CoursesTOP
  • TutorialsNEW
    • Python Tutorial
    • Python Examples
    • C Programming
    • Java Programming
    • MySQL Tutorial
    • Selenium Tutorial
    • Selenium Python
    • Playwright Python
    • Software Testing Tutorial
    • Agile Concepts
    • Linux Concepts
    • HowTo Guides
    • Android Topics
    • AngularJS Guides
    • Learn Automation
    • Technology Guides
  • Top Interviews & Quizzes
    • SQL Interview Questions
    • Testing Interview Questions
    • Python Interview Questions
    • Selenium Interview Questions
    • C Sharp Interview Questions
    • Java Interview Questions
    • Web Development Questions
    • PHP Interview Questions
    • Python Quizzes
    • Java Quizzes
    • Selenium Quizzes
    • Testing Quizzes
    • HTML CSS Quiz
    • Shell Script Quizzes
  • ToolsHOT
    • Python Online Compiler
    • Python Code Checker
    • C Online Compiler
    • Review Best IDEs
    • Random Letter Gen
    • Random Num Gen
Follow US
© TechBeamers. All Rights Reserved.
Selenium Tutorial

Zoom In and Zoom Out in Selenium WebDriver

Last updated: Mar 09, 2025 12:25 am
Meenakshi Agarwal
By
Meenakshi Agarwal
Meenakshi Agarwal Avatar
ByMeenakshi Agarwal
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large...
Follow:
No Comments
3 months ago
Share
7 Min Read
SHARE

Problem – How to Zoom In/Out in Webdriver

In this tutorial, we’ll explore how to zoom in and out in Selenium Webdriver using Java. This functionality is crucial for responsive designs or capturing full-page screenshots. We’ll discuss two methods, compare their pros and cons, and guide you to choose the best approach for your automation needs.

Contents
Problem – How to Zoom In/Out in WebdriverPrerequisites for Selenium JavaSolutions for Zoom In / Zoom OutGeneral QuestionsBefore You Leave
How to Zoom In and Zoom Out in Selenium WebDriver

Prerequisites for Selenium Java

Before diving into the tutorial, ensure you have the following prerequisites:

  • Java Installed: Selenium WebDriver works seamlessly with Java. Make sure you have Java installed on your system.
  • Selenium WebDriver Library: Download the Selenium WebDriver library for Java.
  • WebDriver Browser Driver: Choose the browser you want to use (e.g., Chrome, Firefox) and download the respective WebDriver executable. Make sure to place it in a directory accessible from your system’s PATH.

Solutions for Zoom In / Zoom Out

Let’s start by setting up a basic Selenium WebDriver project. Create a new Java project in your favorite IDE (Eclipse, IntelliJ, etc.) and add the Selenium WebDriver library to your project’s build path.

Using JavaScript Executor

The first method involves using JavascriptExecutor to execute JavaScript code that adjusts the zoom level. Let’s create a Java class named ZoomInOutJS to implement this method:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.JavascriptExecutor;

public class ZoomInOutJS {

    public static void main(String[] args) {
        // Set up the ChromeDriver
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        // Start the Chrome browser
        WebDriver driver = new ChromeDriver();

        // Open a website
        driver.get("https://example.com");

        // Zoom in the webpage
        zoomIn(driver);

        // Pause to see the zoom effect
        pause(2000);

        // Zoom out the webpage
        zoomOut(driver);

        // Close the browser
        driver.quit();
    }

    // Function to zoom in
    private static void zoomIn(WebDriver driver) {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("document.body.style.zoom = '120%';");
    }

    // Function to zoom out
    private static void zoomOut(WebDriver driver) {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("document.body.style.zoom = '80%';");
    }

    // Function to pause the code
    private static void pause(int ms) {
        try {
            Thread.sleep(ms);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Pros:

  • The code changes the zoom level directly using JavaScript, which can be precise.
  • It allows for detailed control over how much to zoom in or out.

Cons:

  • It might not work well with some page elements or layouts.
  • Certain web pages may not display correctly when zoomed this way.

Using Browser Functionality

The second method involves using keyboard shortcuts to trigger the browser’s built-in functionality. Let’s create a Java class named ZoomInOutBrowser to implement this method:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.Keys;

public class ZoomInOutBrowser {

    public static void main(String[] args) {
        // Set up the ChromeDriver
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        // Start the Chrome browser
        WebDriver driver = new ChromeDriver();

        // Open a website
        driver.get("https://example.com");

        // Zooming in the browser window
        zoomIn(driver);

        // Pause to see the zoom effect
        pause(2000);

        // Zooming out the browser window
        zoomOut(driver);

        // Close the browser
        driver.quit();
    }

    // Function to zoom in
    private static void zoomIn(WebDriver driver) {
        Actions act = new Actions(driver);
        act.keyDown(Keys.CONTROL).sendKeys(Keys.ADD).keyUp(Keys.CONTROL).perform();
    }

    // Function to zoom out
    private static void zoomOut(WebDriver driver) {
        Actions act = new Actions(driver);
        act.keyDown(Keys.CONTROL).sendKeys(Keys.SUBTRACT).keyUp(Keys.CONTROL).perform();
    }

    // Function to pause the code
    private static void pause(int ms) {
        try {
            Thread.sleep(ms);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Pros:

  • This code uses the browser’s own zoom feature, which makes it easy to increase or reduce the view.
  • It simulates how a real user would zoom, giving a realistic test experience.

Cons:

  • The code might not work the same way in all browsers.
  • It depends on keyboard shortcuts, which can be different in some browsers.

More Related Topics:
How to Generate Extent Report in Selenium with Python, Java, and C#
Cypress vs Selenium – Which One Should You be Using in 2024
How to Use Selenium IDE to Add Input-based Test Cases
When Selenium Click() not Working – What to do?

General Questions

Here are some of the common queries on this topic.

Q1: Why is zooming important in Selenium WebDriver?

A1: It is important to test how a website looks on different screen sizes. It helps ensure the site works well when the screen size is large or small.

Q2: Can I zoom to a specific percentage using these methods?

A2: Yes, you can set to any percentage you want by adjusting the values in the code.

Q3: Does browser zoom affect automated testing?

A3: Yes, it changes how elements appear on the page. Testing with different levels ensures the site works on all screen sizes.

Q4: Can I use these methods for other browsers?

A4: Yes, these methods can be used in any browser. Just make sure to set up the WebDriver for the browser you’re using.

Q5: How can I zoom in headless mode?

A5: These methods work in both regular and headless modes. Run your tests in headless mode, and zooming will still work.

Additional Resources:
Handle iFrame/iFrames in Selenium Webdriver
Use Selenium WebDriver Waits in Python
How to Locate Elements using Selenium Python

Before You Leave

You’ve explored two methods to zoom in/out on a webpage using Selenium WebDriver in Java. Each method has its pros and cons, and the choice depends on your specific testing requirements. Experiment with both approaches to determine which one aligns better with your automation needs.

TechBeamers will keep providing new tutorials in the near future. In the meantime, please keep reading, learning, and subscribe to our YouTube channel.

Happy testing!

Related

TAGGED:Selenium Action Class
Share This Article
Flipboard Copy Link
Subscribe
Notify of
guest

guest

0 Comments
Newest
Oldest
Inline Feedbacks
View all comments

List of Topics

Stay Connected

FacebookLike
XFollow
YoutubeSubscribe
LinkedInFollow

Subscribe to Blog via Email

Enter your email address to subscribe to latest knowledge sharing updates.

Join 1,011 other subscribers

Continue Reading

  • How do I Merge Two Extent Reports?Feb 11
  • Difference Between Extent Report and Allure ReportFeb 11
  • How to Inspect Element in Safari, Android, and iPhoneFeb 11
  • Selenium Version 4 Features – What’s New?Feb 12
  • Selenium 4 Relative Locators GuideFeb 14
  • Implement Page Object Model in SeleniumAug 17
  • Selenium Java App to Find Blog Rank in GoogleOct 12
  • Best Selenium Practice Websites in 2025Feb 8
  • Six Steps To Setup Selenium WebDriver Project in EclipseMar 1
  • Selenium IDE Download And Installation GuideMar 12
View all →

RELATED TUTORIALS

How to Use Chrome for Running Webdriver Test Cases

How to Use Chrome to Run Webdriver Test Cases

By Meenakshi Agarwal
2 weeks ago
Selenium Version 4 Features - What's New?

Selenium Version 4 Features – What’s New?

By Meenakshi Agarwal
12 months ago
Selenium Components for Web Test Automation

Selenium Components for Web Test Automation

By Harsh S.
1 year ago
Selenium IDE Add-ons for Firefox

4 Must-have Selenium IDE Add-ons for Firefox

By Meenakshi Agarwal
9 months ago
© TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
wpDiscuz