[英]How to “scroll up” in div using selenium in python?
I am trying to scrape job details in LinkedIn.我正在尝试在 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.现在我想向上滚动到 div 的顶部。 I want to scroll up in div shown as red rectangle:
我想在显示为红色矩形的 div 中向上滚动:
Scrolling to top of the page in Python using Selenium 使用 Selenium 滚动到 Python 中的页面顶部
Scroll Element into View with Selenium 使用 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首先,您可以在 DOM 中定位元素(在 DOM 视图的顶部),然后向上滚动直到找到该元素
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其次,您可以在Keys的帮助下轻松使用CTRL+HOME滚动到页面顶部
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.但是在您处理 JavaScript DOM 的情况下,推荐的方法是第一种。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.