@@ -478,21 +478,27 @@ public void scrollToCenter(WebDriver webDriver, WebElement webElement, Point off
478478 * Also working with elements in frames/iframes
479479 */
480480 public void scrollToCenter (UiElement uiElement , Point offset ) {
481+ // Get location via JSUtils method to support frames/iframes
482+ Point elementLocationInParent = new JSUtils ().getElementLocationInParent (uiElement );
483+
484+ final String js = String .format (
485+ "const elementRect = arguments[0].getBoundingClientRect();" +
486+ "const scrollX = %d + (elementRect.width /2) - (window.innerWidth / 2);" +
487+ "const scrollY = %d + (elementRect.height / 2) - (window.innerHeight / 2);" +
488+ "window.scrollBy(scrollX + %s, scrollY + %s)" ,
489+ elementLocationInParent .getX (),
490+ elementLocationInParent .getY (),
491+ offset .getX (),
492+ offset .getY ());
493+
481494 uiElement .findWebElement (webElement -> {
482495 JSUtils .executeScript (
483496 uiElement .getWebDriver (),
484- "arguments[0].scrollIntoView({ block: 'center', inline: 'center' });" ,
497+ js ,
485498 webElement
486499 );
487500 });
488501
489- JSUtils .executeScript (
490- uiElement .getWebDriver (),
491- "window.scrollBy(arguments[0], arguments[1]);" ,
492- offset != null ? offset .getX () : 0 ,
493- offset != null ? offset .getY () : 0
494- );
495-
496502 }
497503
498504 /**
0 commit comments