1

I have a scenario where system needs to scroll UP to the webelement in the left panel of the page and click to perform other operations.I have tried the below ways but none of them are working.Please suggest :

1.

 WebElement element = driver.findElement(By.xpath("element"));
    ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
    Thread.sleep(500); 

2.

WebElement element = driver.findElement(By.xpath("element"));
 Actions actions = new Actions(driver);
        actions.moveToElement(ele);
        actions.perform();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Rahul
  • 759
  • 3
  • 21
  • 43
  • What the error you getting? – frianH Sep 11 '19 at 09:48
  • @frianH : am getting this error " org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[text()='some text']"} " – Rahul Sep 11 '19 at 09:53
  • Can you try to modify your locator to .//*[contains(text(),'some text')] – work_ishaan Sep 11 '19 at 09:56
  • @Rahul Are you sure with your locator ? it seem like element not found in the pages. Please share html. – frianH Sep 11 '19 at 09:58
  • @frianH : It is working fine until the element is visible i.e, when other text is being added below that element , webdriver is unable to scroll up to that desired element and perform actions.sorry, html cannot be shared as it is private. – Rahul Sep 11 '19 at 10:02

1 Answers1

2

Up or Down to scrollIntoView() an element you have to induce WebDriverWait for the visibilityOfElementLocated() and you can use the following solution:

((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("element"))));
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352