Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium WebDriver zoom in/out page content

How to change page zoom level in Selenium WebDriver? I tried:

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

But it doesn't work.

like image 260
user2099471 Avatar asked Feb 22 '13 12:02

user2099471


People also ask

How do I zoom out in chrome Selenium?

As we know Selenium automates the browsers. Many times when we running scripts in selenium, there is a need to handle the coordinates of the browser and requires zoom in and zoom out. If we talk about manually, we could do that by pressing Ctrl + ADD for zoom in and Ctrl + SUBTRACT for zoom out.

How do I get text to full page in Selenium?

New Selenium IDE There are more than one ways of achieving it. To get the text of the visible on the page we can use the method findElement(By. tagname()) method to get hold of . Next can then use the getText() method to extract text from the body tag.

How do I resize an element in Selenium?

To automate resizing functionality, you can combine the ClickAndHold() and MoveByOffset() methods of Actions class to achieve that. public Actions ClickAndHold(IWebElement onElement); public Actions MoveByOffset(int offsetX, int offsetY); The ClickAndHold() method needs the current web element as a parameter.

What is the use of JavascriptExecutor in Selenium WebDriver?

What is JavascriptExecutor in Selenium? In simple words, JavascriptExecutor is an interface that is used to execute JavaScript with Selenium. To simplify the usage of JavascriptExecutor in Selenium, think of it as a medium that enables the WebDriver to interact with HTML elements within the browser.


2 Answers

Beware that Selenium assumes the zoom level is at 100%! For example, IE will refuse to start (throws an Exception) when the zoom level is different, because the element locating depends on this and if you changed the zoom level, it would click on wrong elements, at wrong places.


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")); 

C#

(since I realized C# bindings don't have the Keys.chord() method)

Or, you can use the Advanced User Interactions API like this (again, Java code, but it should work the same in C#):

WebElement html = driver.findElement(By.tagName("html"));  new Actions(driver)     .sendKeys(html, Keys.CONTROL, Keys.ADD, Keys.NULL)     .perform(); 

Again, don't forget to reset the zoom afterwards:

new Actions(driver)     .sendKeys(html, Keys.CONTROL, "0", Keys.NULL)     .perform(); 

Note that the naïve approach

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

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

like image 130
Petr Janeček Avatar answered Oct 15 '22 06:10

Petr Janeček


Here are two ways the zoom level can be altered with Java (one is for Chrome and the other is for Firefox):


Chrome

When using v̲e̲r̲s̲i̲o̲n̲ ̲3̲.̲3̲.̲1 of the Selenium Java Client Driver and C̲h̲r̲o̲m̲e̲D̲r̲i̲v̲e̲r̲ ̲2̲.̲2̲8, the following works (where the number in single quotes represents the zoom level to use; 1 = 100%, 1.5 = 150%, etc.):

JavascriptExecutor executor = (JavascriptExecutor)driver; executor.executeScript("document.body.style.zoom = '1.5'"); 

Firefox

The zoom level can be modified with the following:
1. The aforementioned Java Client Driver
2. G̲e̲c̲k̲o̲D̲r̲i̲v̲e̲r̲ ̲v̲0̲.̲1̲5̲.̲0
3. These classes:
java.awt.Robot
java.awt.event.KeyEvent

First of all, instantiate the Robot class:

Robot robot = new Robot(); 

This code causes the zoom level to decrease:

robot.keyPress(KeyEvent.VK_CONTROL); robot.keyPress(KeyEvent.VK_MINUS); robot.keyRelease(KeyEvent.VK_CONTROL); robot.keyRelease(KeyEvent.VK_MINUS); 

This code causes the zoom level to increase:

robot.keyPress(KeyEvent.VK_CONTROL); robot.keyPress(KeyEvent.VK_EQUALS); robot.keyRelease(KeyEvent.VK_CONTROL); robot.keyRelease(KeyEvent.VK_EQUALS); 
like image 32
User253489 Avatar answered Oct 15 '22 04:10

User253489