Selenium如何滚动到页面顶部

3

我想尝试在Whatsapp聊天中向上滚动,以便记录整个聊天内容(保存为csv文件)。

chrome_options = Options()
chrome_options.add_argument("--user-data-dir=chrome-data") #saves login 
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('https://web.whatsapp.com/')
#choose converstaion
#scroll to top not working

这是我尝试过但没有起作用的方法。理想情况下,我希望能够无界面运行,因此不想使用pyautogui
def scroll_to_top():
    driver.execute_script("window.scrollTo(0, -document.body.scrollHeight);")
    time.sleep(4)

#driver.execute_script("window.scrollTo(0, 0);")
#bd = driver.find_element_by_xpath('//body')
#bd.click()
#bd.send_keys(Keys.CONTROL+Keys.HOME) #throws element not interact-able

欢迎任何帮助

1个回答

2

请使用以下代码滚动到页面顶部

解决方法1:

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

解决方案2:

from selenium.webdriver.common.keys import Keys
html = driver.find_element_by_tag_name('html')
html.send_keys(Keys.PAGE_UP) 

没有一个对我起作用。 - hadesfv
有两个滚动条,一个是用于实际驱动程序的,另一个是用于聊天的。 - hadesfv

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接