javascriptjavagoogle-chromezoomingselenium4

Unable to change the zoom level in Selenium 4


I have just migrated my existing code to selenium4.Disable of automation information bar has been made mandate in the latest browser versions so I'm trying to change the zoom level for a particular page through scripting, but it does not seem to work on my machine. I have tried below piece of codes-

1)

JavascriptExecutor jse= (JavascriptExecutor)driver;
jse.executeScript("document.body.style.zoom='90%'");
WebElement html= driver.findElement(By.tagName("html"));
html.sendKeys(Keys.chord(Keys.CONTROL,Keys.SUBTRACT));

Both of them are not working. After trying to evaluate the expression in Intellij during runtime, I'm getting null for the second line of code in the 1st code whereas it is coming as undefined in the second line of code for the 2nd code.

I cannot share the complete code snippet or application image considering I'm working on VDI.

I'm trying to understand the cause of these piece of codes not working on my machine. Has anyone faced a similar issue or knows about this?

Let me know for any more details.


Solution

  • I just tried code #1 above and it works , Please note that the zoom setting in Chrome will still show 100% even though the page has actually zoomed out. You can test this by increasing the zoom level to 50%, which should make it more obvious that the page has indeed zoomed out.

    JavascriptExecutor jse= (JavascriptExecutor)driver;
    jse.executeScript("document.body.style.zoom='50%'");