Last active
July 9, 2020 12:27
-
-
Save navchandar/b563198cd3d44595a00452d72328c6c1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| JavascriptExecutor js = (JavascriptExecutor) driver; | |
| // Method 1 | |
| String scrollElementIntoMiddle = "var viewPortWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);" + "var viewPortHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);" + "var elementTop = arguments[0].getBoundingClientRect().top;" + "var elementLeft = arguments[0].getBoundingClientRect().left;" + "jQuery('div').scrollTop(elementTop-(viewPortHeight/2));" + "jQuery('div').scrollLeft(elementLeft-(viewPortWidth/2));"; | |
| js.executeScript(scrollElementIntoMiddle, element); | |
| // Method 2 | |
| String scrollElementWithVanillaJS = "arguments[0].scrollIntoView({behavior:\"smooth\", block:\"center\", inline:\"center\"});"; | |
| js.executeScript(scrollElementWithVanillaJS, element); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment