I have a list of webelements, I have written the below code:

    List <WebElement> feature_tiles = driver.findElements(By.xpath("//div[contains(@class,'featuretile')]//a"));
    System.out.println("" +feature_tiles.size() );
    for(int i=0; i<feature_tiles.size();i++)
        double x = feature_tiles.get(i).getLocation().getX();
        double y = feature_tiles.get(i).getLocation().getY();
        JavascriptExecutor javScriptExecutor = (JavascriptExecutor) driver;

        javScriptExecutor.executeScript("window.scrollBy(" + x + ", " + y + ");")

what I want to do is, scroll to that particular element and wait for it to load and also take a screen shot, of the individual elemetns.

Current is scrolling to the elements even if the images of the elements is not loaded correctly.

  • Welcome. So far, it seems it's working correctly. Selenium will scroll to any x,y location regardless if an element is there or not. Right now, your code doesn't show any waits or screenshot. So, if you want it to wait after the scroll, you'll need to add that. If you do have that, please update your example.
    – Lee Jensen
    Jun 7, 2021 at 22:08
  • which website please add more details
    – PDHide
    Jun 8, 2021 at 16:53

2 Answers 2


Try this

WebElement element = driver.findElement(By.id("mousehover"));
        js.executeScript("arguments[0].scrollIntoView(true);", element);
        js.executeScript("window.scrollBy(0, -190);");

For Screenshot, you have to use TakeScreenshot

File sourceFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(sourceFile, new File(directory + filename));

This scenario is faced by most of the software testing service companies to verify elements visually through automation. In this case instead of taking complete screenshot they prefer to have desired webelement screenshot. In C# we use 'Ashot' library by which we can capture screenshot of complete webpage by scrolling to bottom of the page if available or we can have screenshot for webElement also.

Refer below syntax for webelement capturing:

Screenshot screenshot = new AShot() .takeScreenshot(driver, webelement); ImageIO.write(screenshot.getImage(), "PNG", new File("{location of the screenshot to be saved}"));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.