带有python硒的execute_script()的ScrollHeight大小返回不变的结果

时间:2018-08-05 14:59:28

标签: python selenium

代码:

enter image description here

结果:

enter image description here

图像内容是我当前的代码。我的目标是在YouTube频道中获取所有视频数据。但是目前我只有30个初始视频数据。执行一次逻辑后,它结束。换句话说,newHeight的大小等于lastHeght的大小。绝对可以,execute_script("window.scrollTo(0, document.documentElement.scrollHeight);")代码可以正确执行一次。为什么会出现此结果?

class Channel:
    def __init__(self):
        self.channelId = ''
        self.Home = ''
        self.Home_title = ''
        self.videoTab = ''
        self.chromedriver = '../chromedriver.exe'
        self.chromeBrowser = webdriver.Chrome(self.chromedriver)

    def __del__(self):
        self.chromeBrowser.quit()

def getVideoData(self):
    self.chromeBrowser.get(self.videoTab)
    lastHeight = self.chromeBrowser.execute_script("return document.documentElement.scrollHeight")
    print('lastHeight : ' + str(lastHeight))
    while True:
        self.chromeBrowser.execute_script("window.scrollTo(0, document.documentElement.scrollHeight);")
        self.chromeBrowser.implicitly_wait(0.5)
        newHeight = self.chromeBrowser.execute_script("return document.documentElement.scrollHeight")
        print('newHeight : ' + str(newHeight))
        if newHeight == lastHeight:
            break
        lastHeight = newHeight
    page = self.chromeBrowser.page_source
    html = BeautifulSoup(page, 'html.parser')
    for href in html.select('#thumbnail'):
        href.get('href')
    for video_title in html.select('#video-title'):
        print(video_title.text)
    print(len(html.select('#video-title')))

0 个答案:

没有答案