0

i tried to make a tool to open youtube videos automatically and also play them. I tried that the script also accept the Cookie policy from youtube witth the accept button. I also tried to change the driver.find_element_by_id with the xPath variable, without any success. There i am know, stuck iat this point. I got these errors: Error: Could not accept cookies: 'WebDriver' object has no attribute 'find_element_by_xpath' Error: Could not play the video: 'WebDriver' object has no attribute 'find_element_by_xpath'

also (if i change xPath to id): Error: Could not open the website with the specified YouTube-Link: 'WebDriver' object has no attribute 'find_element_by_id'

Can someone help me out pls? my code is following:

Versions of Software: Chromedriver 115.0.5790.102, Chrome 115.0.5790.110, Selenium v3.141.0

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time

def accept_cookies(driver):
    accept_button = driver.find_element_by_id('//*[@id="content"]/div[2]/div[6]/div[1]/ytd-button-renderer[2]/yt-button-shape/button')
    accept_button.click()

def play_youtube_video(video_link):
    options = Options()
    options.add_argument("--start-maximized")
    driver = webdriver.Chrome(options=options)
    driver.get(video_link)
    time.sleep(5)  # Warten Sie einige Sekunden, um das Video zu laden

    accept_cookies(driver)  # Akzeptieren Sie die Cookies

    try:
        play_button = driver.find_element_by_xpath('//*[@aria-label="Play"]')
        play_button.click()
        print("Viewing process has started.")
    except Exception as e:
        print(f"Error 01: Could not play the video: {e}")
        driver.quit()
        return None
    return driver

if __name__ == "__main__":
    video_link = input("Geben Sie den YouTube-Video-Link ein: ")
    try:
        driver = play_youtube_video(video_link)
        while True:
            # Öffnen Sie ein neues Fenster und spielen Sie das Video ab
            driver.execute_script("window.open('','_blank');")
            driver.switch_to.window(driver.window_handles[-1])
            driver.get(video_link)
            time.sleep(5)
            accept_cookies(driver)
            try:
                play_button = driver.find_element_by_xpath('//*[@aria-label="Play"]')
                play_button.click()
                print("Viewing process has started.")
            except Exception as e:
                print(f"Error 01: Could not play the video: {e}")
                driver.quit()
                break
    except Exception as e:
        print(f"Error: Could not open the website with the specified YouTube-Link: {e}")

to solve the error with the cockie policy.

Luis
  • 11
  • 3

0 Answers0