Can not make Selenium's browser zoom functions work

1,584 views
Skip to first unread message

Raymond Rudd

unread,
May 26, 2020, 12:51:15 PM5/26/20
to Selenium Users
I have a need programatically zoom the browser to 90%.  It is because of a certain bug in the application code which should be fixed in time but might take a long time. I have tired at least three methods as follows to do the zooming but none actually do anything for me except for the js method which I've been able to get to work only in Chrome.  I'm also aware of statements I found in my searches indicating that Selenium assumes a 100% zoom level and that it might have trouble finding elements if the zoom is changed. Chrome seems to fit this since when I zoom it to 90% it has trouble finding some elements.  I have found FF to be somewhat more robust after it is zoomed to 90%, but I have not been able to get any of the following methods to do anything to FF/Gecko programatically.  I've read a number of posts.  Most of them cover these methods and a few more.  I've not been able to get any of the to work for FF.  I wonder if there is something I'm missing or if the selenium browser drivers just don't allow it anymore.  Sure would appreciate any help   Thx :) 

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


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


wait.until(elementToBeClickable(By.tagName("html"))).sendKeys(Keys.chord(Keys.CONTROL, Keys.SUBTRACT));


Robot robot = new Robot();
//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);
}



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

Surbhi Nahta

unread,
Jun 3, 2020, 3:35:10 AM6/3/20
to Selenium Users

Hi,

Selenium web driver is one of the best tool widely used by organizations which provides qa testing services. When user run scripts, sometimes a situation occurs where zoom in and zoom out needs to be performed. Selenium provides multiple ways to do zoom in and zoom out by "Actions" or "Robot" class or by sendKeys method. However, in some browsers it does not work properly. Another way to do zoom in and zoom out is by using JavascriptExecutor. For example :

public void hitUrl() {
String exePath = "./Driver/chromedriver.exe";
System.setProperty("webdriver.chrome.driver", exePath);
driver = new ChromeDriver();
driver.get("http://toolsqa.wpengine.com/automation-practice-form/");
driver.manage().window().maximize();
zoomInZoomOut("150%");
zoomInZoomOut("80%");
zoomInZoomOut("100%");
}

public void zoomInZoomOut(String value){
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.body.style.zoom='" + value +"'");
}


100% is the normal size of window. For zoom in, provide any value greater than 100 and for zoom out, less than 100. User can also set the size of window as per its requirement.

Dimension originalDim = driver.manage().window().getSize();
driver.manage().window().setSize(new Dimension(1500, 1000));

Here the variable 'originalDim' has the actual value of window's height and width.You can also set the size as defined in below example.

driver.manage().window().setSize(new Dimension(originalDim.getWidth() + 100, originalDim.getHeight() + 50));
driver.manage().window().setSize(new Dimension(originalDim.getWidth() - 200, originalDim.getHeight() - 50));


For more information, connect to Selenium classes in Pune.


Thanks.

⇜Krishnan Mahadevan⇝

unread,
Jun 3, 2020, 4:10:43 AM6/3/20
to Selenium Users
Surbhi,

I notice that you have camouflaged the evangelising in your answer :)

Please DONOT use this forum for any sort of advertisements/evangelising trainings etc., Please help keep this forum relevant by posting queries that are ONLY related to Selenium/WebDriver.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribblings @ https://rationaleemotions.com/


--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/44a6f15c-30b3-4e9f-9a79-1e7e75c83f5d%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages