python - 等待页面加载使用 Selenium 显式等待python

标签 python selenium selenium-webdriver youtube

我是针对Windows使用python3的初学者。
我的问题是我正在尝试从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)

请帮忙。

最佳答案

您必须等到视频标题已更改。请参阅下面的代码。我创建了一个自定义的等待类,以比较新标题和旧标题。
但是,您可能会遇到一个广告视频,该视频将阻止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
    ...

关于python - 等待页面加载使用 Selenium 显式等待python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48510411/

相关文章:

python - T2.micro running python scraper - 无法控制的CPU

java - 使用 selenium 将基于关键字的 Excel 表测试用例作为 TestNG 测试用例执行

java - 我的 appium 测试在一台设备上运行两次,但我需要在两台设备上并行运行它

python Selenium : iterate through radio buttons that have dynamic ids and select

python - 将matplotlib颜色图应用于opencv图像

python gtk 切换组合框?

Firefox 37 和 Selenium Webdriver 2.45

android - 在 Android 手机上执行 Selenium WebDriver 脚本

python - 如何为极坐标图中的圆形线着色(matplotlib)

python - shell 脚本不能与 nohup 一起工作