Zoom In and Zoom out using selenium

Viewed 12380

I have a requirement, where I need to perform Zoom In and Zoom out using selenium webdriver. So this is the short cut key that i need to perform. Control+Shift+Add Key.

I tried the below code in chrome browser. But unable to perform action. Could anyone have the right solution?

Actions actions = new Actions(driver);
actions.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT).sendKeys(Keys.chord(Keys.ADD)).keyUp(Keys.CONTROL).keyUp(Keys.SHIFT).perform();

Chrome Version - 62
Selenium Version - 2.53
OS - Windows 7 and 10

3 Answers

If you want to do the zoom in chrome (not with the css) you can use the API. For example:

driver.get('chrome://settings/')
driver.execute_script('chrome.settingsPrivate.setDefaultZoom(1.5);')
driver.get("https://www.google.co.uk/")

EDIT

In Java:

System.setProperty("webdriver.chrome.driver", /pathTo/chromeDriver);
ChromeDriver driver = new ChromeDriver();
driver.get("chrome://settings/");
driver.executeScript("chrome.settingsPrivate.setDefaultZoom(1.5);");
driver.get("https://www.google.co.uk/");

If you are using headless chrome, you can add --force-device-scale-factor=1.5 argument to change the zoom level

You can use javascript to do this action (using css zoom):

For example (in python):

driver.execute_script("document.body.style.zoom = '200%'")

It works at least with Chrome.

Related