JavaScriptExecutor scroll into View is not working in my TestNG Automation

646 views Asked by At

I am using the below code-

WebElement ele= driver.findElement(By.xpath("//span[text()='Select Selection Field 3']"));
 ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", ele);
     

And it doesn't perform any function. I dont get any error but scrolling is becoming a blocker for me. Please Help!!

1

There are 1 answers

0
undetected Selenium On

Before you attempt to scrollIntoView() you need to induce WebDriverWait for the visibilityOfElementLocated() as follows:

WebElement ele = new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='Select Selection Field 3']")));
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", ele);

In a single line:

((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView(true);", new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='Select Selection Field 3']"))));