To zoom in webpage on browser I searched solution and used two ways but on both I am receiving an error can someone please help me identify where is the mistake or whats wrong with the code. Browser I am using is Chrome

First method:

element = driver.findElement(By.tagName("html"));
element.sendKeys(Keys.chord(Keys.CONTROL, Keys.ADD));

Error received on this: Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: cannot focus element

To solve above error I tried:

 element = driver.findElement(By.tagname("html"));
        Actions actions = new Actions(driver);
        actions.sendKeys(Keys.CONTROL, Keys.ADD);

Error received: unknown error: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'.

Also I tried to replace element "html" with "body" but zoom not worked

  • You should use Javascript Executor to perform this action. See Raja Sekhar's answer. – demouser123 Mar 31 '17 at 11:47
  • Suggested answer isn't working for me. No zooming effect can be seen – Rabia Asif Mar 31 '17 at 13:20
  • @bad_deadpool can you please help? – Rabia Asif Mar 31 '17 at 13:32

When using v̲e̲r̲s̲i̲o̲n̲ ̲3̲.̲3̲.̲1 of the Selenium Java Client Driver and C̲h̲r̲o̲m̲e̲D̲r̲i̲v̲e̲r̲ ̲2̲.̲2̲8, the following works (where the number in single quotes represents the zoom level to use; 1 = 100%, 1.5 = 150%, etc.):

JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("document.body.style.zoom = '1.5'");

In addition, the zoom level in Firefox can be modified with the following:
1. The aforementioned Java Client Driver
2. G̲e̲c̲k̲o̲D̲r̲i̲v̲e̲r̲ ̲v̲0̲.̲1̲5̲.̲0
3. These classes:

First of all, instantiate the Robot class:

Robot robot = new Robot();

This code causes the zoom level to decrease:


This code causes the zoom level to increase:

| improve this answer | |
  • This definitely works for chrome. Is there any way for firefox? – Rabia Asif Apr 3 '17 at 5:12
  • @RabiaAsif Yes. I added a Firefox solution to my original answer. – User253489 Apr 11 '17 at 4:47

it should work (work around)

JavascriptExecutor js = (JavascriptExecutor) driver;
| improve this answer | |
  • This isn't working – Rabia Asif Mar 31 '17 at 12:37
  • hi, this is javascript based it should work most probably, if still not working... once check whether or not you initiated the javascriptExecutor properly, hope this can help you, and please try @User253489's answer below as well cheers. – Raja Sekhar Apr 3 '17 at 4:16
  • answer by User253489 works for me on chrome only. How to make that work for firefox? – Rabia Asif Apr 3 '17 at 5:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.