最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

selenium webdriver - Play YouTube video until finished and exit while loop - Stack Overflow

programmeradmin5浏览0评论

I am new to Selenium and am trying to use it to search and play an entire YouTube video. I found this while searching for solutions and tried to edit the code to stop the loop when the "Play next video" button appears, but I cannot get it to stop the loop.

This is my code:

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


def main():
    search = input("Please enter your search:\n")
    driver = webdriver.Chrome()

    driver.get(f"={search}")

    time.sleep(4)
    titles = driver.find_element(By.CSS_SELECTOR, f'a[id="video-title"]')

    driver.execute_script(f"window.open('{titles.get_attribute('href')}')")
    wait = WebDriverWait(driver,5)
    while True:
        try:
            wait.until(EC.element_to_be_clickable((By.XPATH,"//button[@aria-label='Play next video']")))
            break
        except:
            pass


if __name__ == "__main__":
    main()
 

Is there something I am doing wrong? Thank you for your time.

Play YouTube video until finished

发布评论

评论列表(0)

  1. 暂无评论