3

How can I reset Chrome's zoom level to Default(100%) using Selenium WebDriver? It's really easy to do using keyboard keys "Ctrl + 0".

However, when I try to emulate this using WebDriver, I can't get anything to work. Here is what I tried without success:

    [TestMethod]
    public void ZoomToDefaultLevelWithChrome()
    {
        var driver = new ChromeDriver();
        driver.Navigate().GoToUrl("https://www.ultimateqa.com");
        var actions = new Actions(driver);
        var browser = (IJavaScriptExecutor)driver;
        var html = driver.FindElement(By.TagName("html"));

        //None of these work
        actions.SendKeys(Keys.Control).SendKeys(Keys.Add).Perform();
        actions.KeyDown(Keys.Control).SendKeys(Keys.NumberPad0).Perform();
        //actions.KeyDown(Keys.Control).SendKeys(Keys.NumberPad0).Perform();
        //actions.KeyDown(Keys.Control).SendKeys(html, "0").Perform();
        actions.KeyUp(Keys.Control);
        actions.KeyDown(Keys.Control).Perform();
        actions.SendKeys(html, "0").Perform();
        actions.SendKeys(html, Keys.NumberPad0).Perform();
        actions.SendKeys(Keys.NumberPad0).Perform();

        browser.ExecuteScript("document.body.style.zoom = '1.0'");
        browser.ExecuteScript("document.body.style.transform='scale(1.0)';");
        browser.ExecuteScript("document.body.style.zoom='100%';");

    }

The first 2 solutions from here don't work in C#: Selenium webdriver zoom in/out page content Also, this doesn't work in Chrome even though it might in other browsers: selenium vba code to zoom out webpage to 60%

Nikolay Advolodkin
  • 1,820
  • 2
  • 24
  • 28
  • How did you endup with a zoom different from 100%? The line `new ChromeDriver()` starts an fresh instance/profile with a default zoom level of 100%. – Florent B. Mar 12 '18 at 18:52
  • This is a test of the different zooming options. My real test actually zooms in on my software under test. – Nikolay Advolodkin Mar 12 '18 at 18:55
  • Possible duplicate of [selenium vba code to zoom out webpage to 60%](https://stackoverflow.com/questions/48436067/selenium-vba-code-to-zoom-out-webpage-to-60) – undetected Selenium Mar 14 '18 at 06:05

2 Answers2

2

Can you try the below. This should work.

((IJavaScriptExecutor)driver).executeScript("document.body.style.zoom='100%';");
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Bendram
  • 120
  • 1
  • 9
0

I have also encountered this problem, a work around I found was good was setting the the resolution capability

capability.SetCapability("resolution", 1920x1080);
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352