gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 06:12:59 26 4
gpt4 key购买 nike

我是针对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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com