Scroll Element into View with Selenium

Scroll Element into View with Selenium

Have tried many things with respect to scroll, but the below code has provided better results.

This will scroll until the element is in view:

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

//do anything you want with the element

You can use the org.openqa.selenium.interactions.Actions class to move to an element.

Java:

WebElement element = driver.findElement(By.id(my-id));
Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.perform();

Python:

from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).move_to_element(driver.sl.find_element_by_id(my-id)).perform()

Scroll Element into View with Selenium

JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript(javascript:window.scrollBy(250,350));

You may want to try this.

Leave a Reply

Your email address will not be published.