2

To zoom in webpage on browser I searched solution and used following ways but I am receiving either an error or no zoom effect. Can someone please help me to identify where is the mistake or what's 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.moveToElement(element);
actions.click();
actions.sendKeys(Keys.CONTROL, Keys.ADD);
actions.build().perform();

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

Third way:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.body.style.zoom='80%'");
NarendraR
  • 7,577
  • 10
  • 44
  • 82
Rabia Asif
  • 298
  • 3
  • 14

3 Answers3

1

I have tested third way in chrome and IE browser, it was working fine.

    driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.get("http://store.demoqa.com/");
    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("document.body.style.zoom='200%'");

zoom is not implemented in Firefox.

The "replacement" is transform from CSS3: https://developer.mozilla.org/En/transform

Below is the code for firefox browser:

    WebDriver driver;    
    driver = new FirefoxDriver();
    driver.manage().window().maximize();
    driver.get("http://store.demoqa.com/");
    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("document.body.style.MozTransform = 'scale(2)';");

Reference: object.Style.Zoom property not working in Firefox

Let me know if you have any queries

Community
  • 1
  • 1
Akarsh
  • 967
  • 5
  • 9
1

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:
java.awt.Robot
java.awt.event.KeyEvent

First of all, instantiate the Robot class:

Robot robot = new Robot();

This code causes the zoom level to decrease:

robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_MINUS);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_MINUS);

This code causes the zoom level to increase:

robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_EQUALS);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_EQUALS);
User253489
  • 169
  • 1
  • 4
0

Here is what you need:

new Actions(driver)
     .sendKeys(Keys.Control).sendKeys(Keys.Add)
     .perform();

At list this works for me on C#.

Denis Koreyba
  • 3,144
  • 1
  • 31
  • 49