0

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!!

Priya
  • 1

1 Answers1

0

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']"))));
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352