【问题标题】:waiting for a page to load using selenium explicit wait python使用硒显式等待python等待页面加载
【发布时间】:2018-07-08 16:31:44
【问题描述】:

我是一个使用python3 for windows的初学者。 我的问题是我试图从 youtube 播放列表中抓取标题和投票(喜欢/不喜欢),并且似乎无法让我的脚本等待下一页加载,然后再对下一页进行投票,直到播放列表结束。

相反,它只取标题,并且在复制所有内容后,第一页的投票重复此操作并仅单击下一页一次。

我用谷歌搜索并查看了其他帖子,认为可能需要调用显式等待,但它似乎仍然不起作用。

当前脚本:

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)

请帮忙。

【问题讨论】:

    标签: python selenium selenium-webdriver youtube


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复! :) 我做了一些挖掘,发现 url_changes 方法真的很有帮助。硒再次很棒!是的,广告有点讨厌......我必须找到与它们有关的东西......
    猜你喜欢
    • 2017-03-15
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    相关资源
    最近更新 更多