0

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
    
1
  • @Codelt 'select_video2=driver.find_element_by_xpath(f"(//a[@id='video-title'])[{video_number}]").click() ' for this how can i send k (stop) button ? simple send keys is not working Commented May 9, 2021 at 10:24

2 Answers 2

5

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()
Sign up to request clarification or add additional context in comments.

Comments

0

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

3 Comments

tried the following but didn't work pause=driver.find_element_by_css_selector('button.ytp-play-button').click() '
This button appears not immediately, only after the video is loaded and started playing. So you need to put a wait before it
put a timer of 5 seconds after playing the video, but didn't work, and don't give error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.