2

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

pii_ke
  • 2,811
  • 2
  • 20
  • 30

2 Answers2

1

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) 
Jortega
  • 3,616
  • 1
  • 18
  • 21
0

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.

  • element = driver.find_element_by_xpath("element_xpath") this code help me. but one thing is in linkedin it is dynamic when u scroll it loads data . so in order to go to top of result i have to scroll 5 time skipping 5 elements at time. Thanks for the solution. – Pritesh Gujarati Jul 17 '20 at 11:05
  • Parallel to you, DOM viewm like Google Maps or Linkedin scroll bars, it is hard to scroll up or down with selenium without core javascript code. Also usually, if you want to scrool to the bottom of the view, you should use while loop until there is no new element in the DOM view. – Abdullah Deliogullari Jul 17 '20 at 13:04