How to "scroll up" in div using selenium in python?

Viewed 1854

I am trying to scrape job details in LinkedIn. As per now, I am able to scroll till the end of list.

code:

#scrolling till buttom of page(online job section)
target = driver.find_element_by_xpath('//div[@class="jobs-search-results jobs-search-results--is-two-pane"]')
time.sleep(1)
driver.execute_script('arguments[0].scrollTop = arguments[0].scrollHeight', target)
driver.implicitly_wait(1)
time.sleep(1)     

now I want to scroll up to the top of the div. I want to scroll up in div shown as red rectangle:

red rectangle showing div

2 Answers

Scrolling to top of the page in Python using Selenium

Scroll Element into View with Selenium

There is 2 main way to do that:

Firstly, you can locate the element(at the top of your DOM view) in the DOM and then scrolling up until you find the element

element = driver.find_element_by_xpath("element_xpath")
driver.execute_script("return arguments[0].scrollIntoView(true);", element)

Secondly, you can easily use CTRL+HOME for scrolling to the top of the page with the help of Keys

driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.HOME)

But in your situation which dealing with JavaScript DOM, recommended way is the first one.

There are many ways to do this, here is one way:

driver.execute_script("window.scrollTo(0, 0);")

Alternatively you can scroll up with the mouse:

import pyautogui
pyautogui.scroll(10) 
Related