enter image description here。
以下是我的python代码。get()
运行良好。
但是在执行execute_script("return document.body.scrollHeight")
之后,lastHeight
返回0
。
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()的正确结果。要显示图像,请单击“在此处输入图像描述”
最佳答案
我假设您正在尝试使用selenium滚动到页面的末尾。在分析问题时,我发现javascript document.body.scrollHeight
有一个错误,对于带有浮动Web元素的YouTube.com之类的页面来说,它是有问题的。请参阅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 is being controlled by automated software
信息栏,请使用以下命令启动浏览器。chrome_options = Options()
chrome_options.add_argument('disable_infobars')
driver = webdriver.Chrome(chrome_options=chrome_options)