Selenium Page down by ActionChains

Question:

I have a problem using function to scroll down using PageDown key via Selenium’s ActionChains in python 3.5 on Ubuntu 16.04 x64.

What I want is that my program scrolls down by PageDown twice, so it reaches bottom at the end and so I can have selected element always visible.
Tried making another function using Keys.END, but it did not work, so I assume it has something to do with ActionChains not closing or something.

The function looks like this:

from selenium.webdriver.common.action_chains import ActionChains

def scrollDown(self):
    body = browser.find_element_by_xpath('/html/body')
    body.click()
    ActionChains(browser).send_keys(Keys.PAGE_DOWN).perform()

and I use it in another file like this:

mod.scrollDown()

The first time I use it, it does scroll down as would if PageDown key would be pressed, while another time nothing happens.
It does not matter where i call it, the second (or third…) time it does not execute.
Tried doing it manually and pressed PageDown button twice, works as expected.
Console does not return any error not does the IDE.

Asked By: Matej

||

Answers:

Maybe, if it has to do with the action chains, you can just do it like this:

    from selenium.webdriver.common.keys import Keys

    body = browser.find_element_by_css_selector('body')
    body.send_keys(Keys.PAGE_DOWN)

Hope it works!

Answered By: Chai

I had to click on the body for the Keys.PAGE_DOWN to work but didn’t need to use the action chain:

from selenium.webdriver.common.keys import Keys

body = driver.find_element_by_css_selector('body')
body.click()
body.send_keys(Keys.PAGE_DOWN)
Answered By: Crystal Taggart
#python
from selenium.webdriver.common.keys import Keys

driver.find_element_by_css_selector('body').send_keys(Keys.PAGE_DOWN)
Answered By: Zahouani Mourad
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.