使用Selenium在Python中滚动到页面顶部

25

使用Python和Selenium时,我遇到了滚动到网页顶部的问题。

由于某些原因,页面加载后会将您带到页面底部(这将得到修复)。但是,当我尝试向顶部滚动时,它不起作用。

我尝试过以下方法:

self.driver.execute_script("scroll(0, -250);")

并且

self.driver.execute_script("scroll(0, 0);")

我尝试定位元素并滚动到它:

self.driver.execute_script("arguments[0].scrollIntoView()", element)

上面的 scrollIntoView() 代码在向下滚动到元素时有效。然而,向上滚动无效。

我尝试了在 Chrome Driver 和 PhantomJs 上运行它。

有什么建议吗?


1
你尝试过在execute_script中添加window.scrollTo(0, 0);吗? - Mangohero1
1
@mangoHero1 我尝试过 self.driver.window.scroll("window.scroll(0, 0);"),但脚本显示以下错误: self.driver.window.scroll("window.scroll(0, 0);") AttributeError: 'WebDriver' 对象没有 'window' 属性 - cmplfore
1
@mangoHero1 抱歉,我看到了错别字并进行了更正,然后执行了以下操作:self.driver.execute_script("window.scroll(0, 0);"),但仍未向上滚动。 - cmplfore
@jlaur 对不起,这是在深夜完成的,我把代码打错了。实际上我尝试过以下代码,但并没有成功:self.driver.execute_script("window.scrollTo(0, 0);") - cmplfore
8个回答

34

您可以简单地使用CTRL + HOME键。它将滚动到页面顶部。

from selenium.webdriver.common.keys import Keys
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.HOME)

2
可能需要先从selenium.webdriver.common.keys导入Keys - ErichBSchulz
@ErichBSchulz,是的,没错。需要导入Keys - SunilThorat
如果浏览器被最小化,这似乎无法正常工作。 - NotAPro
如果你收到以下警告:DeprecationWarning: find_element_by_* commands are deprecated. Please use find_element() instead请将代码更改为: from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By driver.find_element(By.XPATH, '//body').send_keys(Keys.CONTROL + Keys.HOME) - Haddock-san

11

你可以先在 HTML DOM 中找到元素,然后我们可以将该元素滚动到 Viewport 中,方法如下:

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

2

有四种方法可以向上或向下滚动

1)按像素滚动

driver.execute_script("window.scrollBy(0,0)","")

2) 向下滚动,直到找不到该元素

element=driver.find_element(By.XPATH,"xpath of element")
driver.execute_script("arguments[0].scrollIntoView();",element)

3) 滚动至页面底部

driver.execute_script("window.scrollBy(0,document.body.scrollHeight)")
  1. 使用动作链

    elementpos=driver.find_element(By.XPATH,"元素的xpath")
    actions=ActionChains(driver) actions.move_to_element(elementpos).perform()


2

您可以使用document.body.scrollTop:它将滚动到页面顶部,无论其高度如何。 像这样:

driver.execute_script("window.scrollTo(0, document.body.scrollTop);")


1
from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("__")

#to scroll try use the following command
driver.execute_script("scrollBy(0,250);")

它会工作!!


3
请补充更多细节到你的回答中。 - Hamza Anis

1

从selenium导入webdriver模块

t=10
while t:

#if you want to scroll to the end of the page,use this

driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")
    sleep(3)


#if you want to scroll down upto some level use this, here i used "1000" you may vary 
#it according to your use

driver.execute_script("scrollBy(0,+1000);")
    sleep(3)


#if you want to scroll some level up, use this,again i used here "-500" you may vary 
#according to your use 

driver.execute_script("scrollBy(0,-500);")
sleep(3)
t=t-1       # it`s a part of the loop

这一定会帮助你 :)

1

您可以使用document.body.scrollTop:一个包含页面滚动前向下滚动的值的JavaScript变量。 像这样: driver.execute_script("scrollBy(0,-document.body.scrollTop)")


1

Please try this:

driver.execute_script("document.querySelector('div[role=dialog] ul').parentNode.scrollTop=1e100")

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