Nikolay Advolodkin
Nikolay Advolodkin

Reputation: 2230

How to use Selenium WebDriver to reset the zoom to default on Chrome?

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%

Upvotes: 3

Views: 2909

Answers (2)

Bendram
Bendram

Reputation: 120

Can you try the below. This should work.

((IJavaScriptExecutor)driver).executeScript("document.body.style.zoom='100%';");

Upvotes: 2

anArchLolz
anArchLolz

Reputation: 1

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

capability.SetCapability("resolution", 1920x1080);

Upvotes: 0

Related Questions