no nein
no nein

Reputation: 711

Scroll down by a certain amount in Selenium, Chrome, Python

I recently upgraded to the new Chromedriver version from a significantly older one and it is giving me a ton of issues with my previous code, one of the more annoying ones is that the .scroll command seems to produce errors now, e.g.:

    scroll_obj=selenium.webdriver.common.touch_actions.TouchActions(driver)
    scroll_obj.scroll(0,scroll_value)
    scroll_obj.perform()

It produces the following error:

WebDriverException: Message: unknown command: Cannot call non W3C standard command while in W3C mode

Is there any similar action I can take where I just scroll down by a fixed amount instead of to a specific document height? I can only find the Javascript solution which scrolls down to a set place.

Upvotes: 0

Views: 2974

Answers (2)

Justin Lambert
Justin Lambert

Reputation: 978

**To scroll down the web page by pixel.**

JavascriptExecutor js = (JavascriptExecutor) driver;
// This  will scroll down the page by  1000 pixel vertical      
js.executeScript("window.scrollBy(0,1000)");

**To scroll down the web page by the visibility of the element.**

JavascriptExecutor js = (JavascriptExecutor) driver;
//Find element by link text and store in variable "Element"             
 WebElement Element = driver.findElement(By.linkText("Linux"));
 //This will scroll the page till the element is found      
 js.executeScript("arguments[0].scrollIntoView();", Element);


**To scroll down the web page at the bottom of the page.**
 JavascriptExecutor js = (JavascriptExecutor) driver;
 js.executeScript("window.scrollTo(0, document.body.scrollHeight)");

Upvotes: 0

Sushil
Sushil

Reputation: 5531

This should help u:

horizontal_scroll = 0

vertical_scroll = 1000

driver.execute_script("window.scrollBy(horizontal_scroll , vertical_scroll );")

Upvotes: 2

Related Questions