enter image description here . 下面是我的python代码。 get() 运行良好。 但是执行execute_script("return document.body.scrollHeight")后,lastHeight返回0。 而 chromedriver 网页上的 Action 是没有的。 然后 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() 的正确结果。要显示图像,请单击“在此处输入图像描述”
请您参考如下方法:
我假设您正在尝试使用 selenium 滚动到页面末尾。在分析这个问题时,我发现 javascript document.body.scrollHeight 存在一个错误,对于像 YouTube.com 这样具有 float Web 元素的页面来说,它会出现故障。引用Bug details .我已经尝试使用 document.documentElement.scrollHeight,它看起来很适合这个页面。
>>> driver.execute_script("return document.documentElement.scrollHeight")
3120
话虽如此,以下指令证明滚动到页面末尾。
>>> height = driver.execute_script("return document.documentElement.scrollHeight")
>>> driver.execute_script("window.scrollTo(0, " + str(height) + ");")
如果您希望删除 Chrome 正在由自动化软件控制 信息栏,请使用以下命令启动浏览器。
chrome_options = Options()
chrome_options.add_argument('disable_infobars')
driver = webdriver.Chrome(chrome_options=chrome_options)
