Reputation: 1
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.webdriver.common.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"https://www.youtube.com/results?search_query={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
Upvotes: 0
Views: 35