1

public void resizeBrowser() {

    try {
        System.out.println("In resize unit");


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


        System.out.println("In resize unit3");

        html.click();
        Robot robot = new Robot();

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

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

It is getting marked duplicate again and again. please try to understand, what i am trying to do is a send keys operation on my FF56 webdriver instance to Zoom in and out. I dont need to set the zoom level of page by injecting JavaScript.

RahulSahay
  • 43
  • 6
  • The issue is that none of the above stated ways are working, JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("document.body.style.transform = 'scale(ZoomPercentage)';"); This too not working – RahulSahay Apr 23 '18 at 10:44
  • When i am performing a manual key press "CTRL" + "ADD/SUB" , then the zoom In/Out is working fine on the webdriver instance, but not through script – RahulSahay Apr 23 '18 at 10:47

1 Answers1

0

The scale() CSS function defines a transformation that resizes an element on the 2D plane - Mozilla

Apparently, document.body.style.zoom='80%'" does not work in Firefox. You need to use the scale(x) function. For example:

document.body.style.transform = 'scale(1.2)'

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.body.style.transform = 'scale(1.2)'");

Some browser does not support it yet. Check this question


Generally it is better to use the scale property instead of the scale() function. I recommend you to give it a try. It may work too.

Update:

I tested this code with selenium version 3.11.0 and geckodriver version 0.20.1. It worked as expected.

mahan
  • 12,366
  • 5
  • 48
  • 83
  • How is your solution different from the dup marked discussion? – undetected Selenium Apr 23 '18 at 09:55
  • JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("document.body.style.transform = 'scale(1.2)';"); - This too not working :-\ – RahulSahay Apr 23 '18 at 10:42
  • actually this zooms in/out that particular page. That too in a different way. The whole webpage comes in center if zoomed out. I want a code that does exactly what "CTRL"+"ADD/SUB" does on a FF web browser. so by that i mean, the code should permanently set a certain zoom level till the time that web driver instance is alive. – RahulSahay Apr 23 '18 at 11:02