为什么执行_script(“returndocument.body.scrollHeight文件)返回0

2024-09-30 01:29:00 发布

您现在位置:Python中文网/ 问答频道 /正文

enter image description here。 下面是我的python代码。 get()运行良好。 但是在执行execute_script("return document.body.scrollHeight")之后,lastHeight返回{}。 而chromedriver网页上的移动则是无。 然后newHeight也返回0。 它们为什么返回0?在

def getVideoData(self):
     self.chromeBrowser.get(self.videoTab)
     lastHeight = self.chromeBrowser.execute_script("return document.body.scrollHeight")
     print(lastHeight)
     while True:
        self.chromeBrowser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        self.chromeBrowser.implicitly_wait(0.5)
        newHeight = self.chromeBrowser.execute_script("return document.body.scrollHeight")
        print(newHeight)
        if newHeight == lastHeight:
           break
        lastHeight = newHeight

图中显示了get()的正确结果。要显示图像,请单击“在此处输入图像说明”


Tags: 图像imageselfexecutegetreturnscriptbody
1条回答
网友
1楼 · 发布于 2024-09-30 01:29:00

我假设您正在尝试使用selenium滚动到页面的末尾。在分析这个问题时,我发现javascript document.body.scrollHeight有一个bug,它在类似于YouTube.com网站有浮动的web元素。参考Bug details。我尝试了document.documentElement.scrollHeight,它看起来对这个页面很有用。在

>>> driver.execute_script("return document.documentElement.scrollHeight")
3120

已经说过,下面的指令可以滚动到页面的末尾。在

^{pr2}$

Scrolled to bottom of page using script

如果要删除Chrome is being controlled by automated software信息栏,请使用以下命令启动浏览器。在

chrome_options = Options()
chrome_options.add_argument('disable_infobars')
driver = webdriver.Chrome(chrome_options=chrome_options)

相关问题 更多 >

    热门问题