Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Page down by ActionChains

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.

like image 744
Matej Avatar asked Sep 13 '16 13:09

Matej


People also ask

How can you scroll the page down in Selenium?

Selenium can execute JavaScript commands with the help of the executeScript method. To scroll down vertically in a page we have to use the JavaScript command window. scrollBy. Then pass the pixel values to be traversed along the x and y axis for horizontal and vertical scroll respectively.

How do I scroll down in sendKeys?

We can perform scroll up/down a page using Actions class in Selenium webdriver. First of all, we have to create an object of this Actions class and then apply the sendKeys method on it. Now, to scroll down a page, we have to pass the parameter Keys. PAGE_DOWN to this method.

What is ActionChains Selenium?

action_chains. ActionChains are a way to automate low level interactions such as mouse movements, mouse button actions, key press, and context menu interactions. ActionChains are a way to automate low level interactions such as mouse movements, mouse button actions, key press, and context menu interactions.


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

like image 166
Chai Avatar answered Oct 02 '22 21:10

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)
like image 41
Crystal Taggart Avatar answered Oct 02 '22 23:10

Crystal Taggart