3
votes

I am loading a webpage in selenium firefox webdriver, then i am zooming in into the page at 500%, and then i wish to take the screenshot of the page in the zoomed in resolution at 500%, but it is not working

DesiredCapabilities desiredCapabilities = DesiredCapabilities.firefox();
desiredCapabilities.setPlatform(Platform.WINDOWS);
WebDriver driver = new FirefoxDriver(desiredCapabilities);
driver.get(url);
driver.manage().window().setSize(new Dimension(Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height));
WebElement html = driver.findElement(By.tagName("html"));
html.sendKeys(Keys.chord(Keys.CONTROL, Keys.ADD));
html.sendKeys(Keys.chord(Keys.CONTROL, Keys.ADD));
html.sendKeys(Keys.chord(Keys.CONTROL, Keys.ADD));
html.sendKeys(Keys.chord(Keys.CONTROL, Keys.ADD));
html.sendKeys(Keys.chord(Keys.CONTROL, Keys.ADD));
html.sendKeys(Keys.chord(Keys.CONTROL, Keys.ADD));
html.sendKeys(Keys.chord(Keys.CONTROL, Keys.ADD));
html.sendKeys(Keys.chord(Keys.CONTROL, Keys.ADD));
html.sendKeys(Keys.chord(Keys.CONTROL, Keys.ADD));

This code zooms into the url. Now i want to take the screenshot in the zoomed in resolution.

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\"+url.substring(url.lastIndexOf("/"),url.lastIndexOf("."))+".png"));

This is taking the screenshot but only at the normal resolution and not the zoomed in resolution.

Please someone help how can it be done in selenium, or even if selenium is the right tools for this. If not then, please suggest something else that is working for my requirement.

2
Can you explain the action for this line? WebElement html = driver.findElement(By.tagName("html"));Rupesh Shinde
@RupeshShinde - I think that is to find whole html page by tag so it says webdriver to zoom whole html page.Helping Hands
@RupeshShinde - HelpingHands is right. It is to fetch the html element, which is the entire page, and them zoom in into the page.Gaurav Bansal
If you switch to using Chrome() instead of Firefox() - does it make any difference? Thanks.alecxe
@alecxe - in chrome zooming not working , it shows error : unknown error: cannot focus elementHelping Hands

2 Answers

1
votes

You can test this:

String script = "var page = this;page.zoomFactor = 0.9;";
((PhantomJSDriver)driver).executePhantomJS(script);

zoomFactor zoomed the screenshot.

0
votes

Below piece of code worked for me.

    File scrFile2 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    // Now you can do whatever you need to do with it, for example copy somewhere
    FileUtils.copyFile(scrFile2, new File("c:\\tmp\\screenshot.png"));

I just opened a page and took screenshot of it in original size and even after I zoomed it by manually and by using your code(For zoom), I got zoomed resolution screenshot.