Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zoom In and Zoom out using selenium

Tags:

java

selenium

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

like image 418
Aishu Avatar asked Sep 11 '25 10:09

Aishu


2 Answers

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

like image 104
ljmocic Avatar answered Sep 13 '25 01:09

ljmocic


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/");
like image 41
Davide Patti Avatar answered Sep 13 '25 01:09

Davide Patti