Scroll Object into view

Had an issue with object being covered by another when I scrolled to Object. Another developer here helped me work through a solutions:

import org.openqa.selenium.JavascriptExecutor
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import com.kms.katalon.core.webui.driver.DriverFactory

	public static void scrollToObject(String valueEdit){
		WebDriver driver = DriverFactory.getWebDriver();
		WebElement element = driver.findElement(By.xpath('//*/table/tbody/tr/td[contains(@id,"Value")][contains(text(), "' + valueEdit + '")]'));
		((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
		Object topOffset = ((JavascriptExecutor) driver).executeScript("return window.pageYOffset");
		((JavascriptExecutor) driver).executeScript("window.scroll(0, " + ((int)topOffset-30) + ");");
	}

What I do is set this into a package and class I import into a test case. This could easily be a new keyword. It uses javascript to scroll to an object then capture the scroll position and move up 30 pixels so you can see it. Obviously this could be a larger number if you want to scroll further or change the “topOffSet” to a positive number if you want to scroll down.

2 Likes