Abu Bakker Hashmi
Abu Bakker Hashmi

Reputation: 59

YouTube automation with python and selenium

I want to automate youtube with selenium and python completely. the following tasks are to be accomplished. the problem is with points number 4 and 6. how to handle them?

  1. open youtube

  2. search any video

  3. play any video

  4. pause the video

  5. like dislike the video.

  6. play the next video

     driver = webdriver.Chrome()
     driver.get('https://www.youtube.com/')
    
     Search_Box = driver.find_element_by_xpath('//*[@id="search"]')   #search bar
     speak("opend sir what shoud i search ?")
     query=takecommand().lower()
     # query=input('data ?') 
     Search_Box.send_keys(query)
     Search_Button=driver.find_element_by_xpath('//*[@id="search-icon-legacy"]') #clicked search button
     Search_Button.click()
     speak("searched now which one ?")
    
    
     query=takecommand().lower()
     # query=input('data ?')
     if "first" in query:
         video_number = 1
         select_video1=driver.find_element_by_xpath(f"(//a[@id='video-title'])[{video_number}]").click()
         # select_video1.click()
     elif 'second' in query:
         video_number = 2
         select_video2=driver.find_element_by_xpath(f"(//a[@id='video-title'])[{video_number}]").click()
         time.sleep(5)
         speak('pausing video')
         select_video2=driver.find_element_by_css_selector('movie_player > div.ytp-chrome-bottom > div.ytp-chrome-controls > div.ytp-left-controls > button').click()  #this was for pause,but didnt worked here
    

Upvotes: 0

Views: 3213

Answers (2)

JD2775
JD2775

Reputation: 3801

Try these...

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

# won't work unless you are logged in
like_btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"(//yt-icon[@class='style-scope ytd-toggle-button-renderer'])[4]")))
like_btn.click()

# won't work unless you are logged in
dislike_btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"(//yt-icon[@class='style-scope ytd-toggle-button-renderer'])[5]")))
dislike_btn.click()

pause_btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//button[@title='Pause (k)']")))
pause_btn.click()

# comment out to test pause btn, otherwise it happens so fast you don't notice
play_btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//button[@title='Play (k)']")))
play_btn.click()

mute_btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//button[@aria-label='Mute (m)']")))
mute_btn.click()

# comment out to test mute_btn, otherwise it happens so fast you don't notice it
unmute_btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//button[@aria-label='Unmute (m)']")))
unmute_btn.click()

Upvotes: 5

Prophet
Prophet

Reputation: 33361

To play or pause video you can click the play / pause button located by the following css_selector: button.ytp-play-button
To go to the next video click the following element a.ytp-next-button. again, it's css-selector

Upvotes: 0

Related Questions