Zoom In and Zoom out using 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();
How To Zoom In And Zoom Out In Selenium Webdriver
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. But in case of automation, we can easily achieve it using Selenium. In this blog, we have two methods for Zoom In and Zoom Out.
Method 1: Using Robot Class
In Java, there is having a class “Robot”. Robot class having the “keyPress” and “ keyRelease ” methods. Using these methods we handle the zoom in and the zoom out.
keyPress() method send an event to press the control, add or subtract key, and keyRelease() method send an event to release the pressed key. VK_CONTROL and VK_ADD used to zoom in and VK_CONROL and VK_SUBTRACT for zoom out.
Method 2: Using sendKeys Method
We can also do the form zoom action using the sendKeys method.
For this, we just need to locate the web element of zoom in and zoom out with the help of the locators. And then we have to use the «Keys.CONTROL» , «Keys.ADD» and «Keys.RELEASE» with the sendKeys().
Code for the Methode 1
package com.general.kairos;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class ZoomInOut @Test
public void googleSearchTest() throws AWTException, InterruptedException
System.setProperty(«webdriver.gecko.driver», System.getProperty(«user.dir»)+»\\src\\test\\java\\drivers\\geckodriver.exe»);
WebDriver driver = new FirefoxDriver();
driver.get(«https://www.softwaretestingmaterial.com»);
driver.manage().window().maximize();
Thread.sleep(5000);
System.out.println(«zooming»);
Robot robot = new Robot();
System.out.println(«About to zoom in»);
for (int i = 0; i < 3; i++) robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ADD);
robot.keyRelease(KeyEvent.VK_ADD);
robot.keyRelease(KeyEvent.VK_CONTROL);
>
Thread.sleep(5000);
System.out.println(«About to zoom out»);
for (int i = 0; i < 4; i++) robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_SUBTRACT);
robot.keyRelease(KeyEvent.VK_SUBTRACT);
robot.keyRelease(KeyEvent.VK_CONTROL);
>
>
>
Code for Method 2
Selenium(WebDriver) + zoom браузера.
![]()
Нашел способ изменения масштаба через JavaScript (Для FireFox — document.body.style.MozTransform = «scale(1.25)»), но при этом масштаб меняется не так, как если бы мы его изменили с помощью CTRL + «+»
Есть ли другой способ изменить масштаб браузера с помощью WebDriver?
#2
achumagin
Отправлено 25 октября 2012 — 08:34
Имитируйте нажатие кнопок ctrl + «+»
ap-test-team.blogspot.com
#3
X-NoNAME
![]()
Отправлено 25 октября 2012 — 09:21
Код ниже не работает:
_driver.findElement(By.tagName("body")).sendKeys(Keys.chord(Keys.CONTROL, "+"));
Думаю, что Браузер не будет реагировать на кнопки отправленные через WebDriver, на эти кнопку будет реагировать только сам документ(JavaScript в документе)
#4
appmen
Отправлено 25 октября 2012 — 10:52
для этого можно использовать Джава робот, думаю, должно заработать
#5
barancev
Отправлено 25 октября 2012 — 11:41
А зачем менять масштаб? Если это сделать — selenium нет будет попадать по ссылкам и кнопкам. На всякий случай предупреждаю 🙂
Алексей Баранцев
Тренинги для тестировщиков (тестирование производительности, защищенности, тест-дизайн, автоматизация):
Линейка тренингов по Selenium
#6
X-NoNAME
![]()
Отправлено 25 октября 2012 — 12:02
для этого можно использовать Джава робот, думаю, должно заработать
Да, с помощью робота — работает:
Robot r = new Robot(); for(int i=0;i <=10;i++)< r.keyPress(KeyEvent.VK_CONTROL); r.delay(50); r.mouseWheel(1); r.delay(50); r.keyRelease(KeyEvent.VK_CONTROL); r.delay(100); >for(int i=0;i
Сейчас ищу простой способ сделать окно браузера активным, без использования JNA.
А зачем менять масштаб? Если это сделать — selenium нет будет попадать по ссылкам и кнопкам. На всякий случай предупреждаю 🙂
Буду проверять смещение элементов друг относительно друга с помощью getLocation().getY(), getX()
Saved searches
Use saved searches to filter your results more quickly
Cancel Create saved search
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Selenide.zoom() scales but behaves differently from user zoom #1598
JensPiegsa opened this issue Oct 11, 2021 · 1 comment
Selenide.zoom() scales but behaves differently from user zoom #1598
JensPiegsa opened this issue Oct 11, 2021 · 1 comment
Comments
JensPiegsa commented Oct 11, 2021
The problem
The zoom() method implementation of Selenide 5.24.4 actually does not behave like Ctrl+mouse wheel zoom in Chrome 94.
but executeJavaScript(«document.body.style.zoom=’125%'»); seems to work.
Details
document.body.style.transform = ‘scale(..)’ just scales but does not take layout into account.
Environment
- Selenide Version: 5.24.4
- Chrome\Firefox\IE Version: 94.0.4606.81 \ — \ —
- Browser Driver Version: 94.0.4606.61
- Selenium Version: 3.141.59
- OS Version: Windows 10
The text was updated successfully, but these errors were encountered:
