0

I could scroll down or up a whole web page but I am having trouble in scrolling a particular div element within the webpage

The thing is when I open a webpage like whatsapp web and within a particular chat containing the messages(which is particular div element),I want to scrape all the messages from the beginning of the chat ,but I could only scrape the messages which is in the view (the last few messages),So I want to scroll that particular div element to the top of the chat to scrape all the messages. Can someone help me with this thing in PYTHON. Thank you.

Dr.Die_OXide
  • 300
  • 2
  • 18

2 Answers2

0

It is possible.
Let's say the element you need to scroll inside is div_el, it's locator is xpath_locator the code will be like this:

div_locator = "xpath_locator"
div_el = driver.find_element_by_xpath(div_locator)
driver.execute_script("arguments[0].scrollTop = arguments[0].scrollHeight", div_el)

See more here

Prophet
  • 32,350
  • 22
  • 54
  • 79
0

Yes, there are multiple ways using which you could scroll to particular element

By using the move_to_element()

 element = driver.find_element_by_xpath("_element_")

 action = ActionChains(driver)
 action.move_to_element(element).perform()

By scrollIntoView()

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

For reference check here

YaDav MaNish
  • 1,260
  • 2
  • 12
  • 20