等待页面加载使用selenium显式wait python

时间:2018-01-29 21:38:00

标签: python selenium selenium-webdriver youtube

我是初学者,使用python3 for windows。 我的问题是我试图从youtube播放列表中删除标题和投票(喜欢/不喜欢)并且似乎无法让我的脚本在下一页上投票之前等待下一页加载,并且直到播放列表结束。

相反,它仅仅取了标题,第一页的投票重复了这一点,并且在复制完所有内容后,只点击下一页一次。

我用Google搜索并查看了其他帖子,认为可能需要调用明确的等待,但它仍然无法正常工作。

当前脚本:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

browser = webdriver.Firefox()
browser.get(
    'https://www.youtube.com/watch?v=2bnMiScBRfQ&list=PLx1Dr6w7DLoLfPixTug9c8xrTkGUsyhkQ&index=')

videosInPlaylist = []

for x in range(1, 4):
    wait = WebDriverWait(browser, 10)
    title = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'h1.title'))).text

    positiveVotes = wait.until(EC.visibility_of_element_located(
        (By.CSS_SELECTOR, 'ytd-toggle-button-renderer.style-text:nth-child(1) > a:nth-child(1) > yt-formatted-string:nth-child(2)'))).text

    negativeVotes = wait.until(EC.visibility_of_element_located(
        (By.CSS_SELECTOR, 'ytd-toggle-button-renderer.style-text:nth-child(2) > a:nth-child(1) > yt-formatted-string:nth-child(2)'))).text

    currentVideo = [title, positiveVotes, negativeVotes]

    nextVideo = wait.until(EC.element_to_be_clickable(
        (By.CSS_SELECTOR, 'ytd-playlist-panel-video-renderer.style-scope:nth-child(3) > a:nth-child(1) > div:nth-child(1) > div:nth-child(3)')))

    videosInPlaylist.append(currentVideo)
    nextVideo.click()
print(videosInPlaylist)

请帮忙。

1 个答案:

答案 0 :(得分:0)

您必须等到视频标题更改为止。请参阅下面的代码。我创建了一个自定义等待类来比较新标题和旧标题。 但是,您可能会遇到阻止nextVideo.click()的广告视频,您需要处理此视频以使此代码完全正常工作。

class element_text_changed(object):
    def __init__(self, locator, oldTitle):
        self.locator = locator
        self.oldTitle = oldTitle

    def __call__(self, browser):
        wait = WebDriverWait(browser, 10)
        titleElement = wait.until(EC.visibility_of_element_located(self.locator))
        newTitle = titleElement.text.strip()
        print("OLD: " + self.oldTitle + ", NEW: " + newTitle)
        if len(self.oldTitle)==0 or (self.oldTitle!=newTitle):
            return titleElement
        else:
            return False

oldTitle = ''
for x in range(1, 7):
    wait = WebDriverWait(browser, 20)
    title = wait.until(element_text_changed((By.CSS_SELECTOR, 'h1.title'), oldTitle)).text
    print(title)
    oldTitle = title

    wait = WebDriverWait(browser, 10)
    positiveVotes = wait.until(EC.visibility_of_element_located(
        (By.CSS_SELECTOR, 'ytd-toggle-button-renderer.style-text:nth-child(1) > a:nth-child(1) > yt-formatted-string:nth-child(2)'))).text
    ...