Set zoom level to 100% in Selenium WebDriver when default is not 100

19.9k Views Asked by At

I'm running into an issue with Selenium WebDriver in Java, where I can't set the IE browser zoom level to 100% when the user has a different default value. Ctrl + 0 does not work since this sets the zoom to the default. I've tried setting zoom through a JavascriptExecutor, as seen in other posts as well. Any help would be appreciated.

5

There are 5 best solutions below

1
On

Try this, you can tweak the calculation which returns the current zoom level, I may be wrong at that

from selenium import webdriver

driver = webdriver.Chrome(executable_path='C:\\Automation Projects\\Selenium Server\\chromedriver.exe')

driver.get('Https://www.google.com')

zoom_level = driver.execute_script('return (window.outerWidth / window.innerWidth)')
if zoom_level > 1:
    driver.execute_script("document.body.style.zoom='90%';")
3
On

Try ignoring the zoom level all together.

DesiredCapabilities caps = DesiredCapabilities.internetExplorer(); caps.setCapability("ignoreZoomSetting", true); driver = new InternetExplorerDriver(caps);

You can see others with the zoom issue here: Similar issue

0
On

From what I googled, you can set the default zoom using the registry. I haven't tried this myself but you should be able to grab the user's default setting, change it to 100% (if it's not there already), do your test, and then restore their default setting.

I haven't tried this myself but it looks pretty straightforward.

HKEY_CURRENT_USERS\SOFTWARE\Microsoft\Internet Explorer\Zoom

Set ZoomFactor to 1000x the factor you want, e.g. 125% is 125000

https://support.microsoft.com/en-us/help/2689447/how-to-set-the-zoom-level-in-internet-explorer-9

1
On

Try Below Code, It is working code

InternetExplorerOptions capabilities= new InternetExplorerOptions();
            capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);
            System.setProperty("webdriver.ie.driver", Constant.drivers + "\\IEDriverServer.exe");
            driver = new InternetExplorerDriver(capabilities);
            driver.manage().window().maximize();
0
On
JavascriptExecutor js =(JavascriptExecutor)driver;
        js.executeScript("document.body.style.zoom='100%'");