Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

How to change the page zoom level in selenium web driver? I tried:

driver.Keyboard().pressKey(Keys.Control);

driver.Keyboard().pressKey(Keys.Add);

But it doesn't work.

1 Answer

0 votes
by (62.9k points)

You must know that the Selenium assumes the zoom level to be at 100%. For example, IE will refuse to start (throws an Exception) when the zoom level is different because the element you are locating solely depends on this and if you have changed the zoom level, it would click on the wrong elements, placed at different places(which is wrong with respect to when zoom is up to 100%)

Java

You can use the Keys.chord() method:

WebElement html = driver.findElement(By.tagName("html"));

html.sendKeys(Keys.chord(Keys.CONTROL, Keys.ADD));

Use cautiously and when you're done, reset the zoom back to 100%:

html.sendKeys(Keys.chord(Keys.CONTROL, "0"));

Note that the naïve approach

html.sendKeys(Keys.CONTROL, Keys.ADD);

doesn't work, because the Ctrl key is released in the sendKeys() method. The WebElement's sendKeys() is different from the one in Actions class methods. Because of which, the Keys.NULL used in this solution is required to be added.

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat Selenium certification!

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...