Skip to content Skip to sidebar Skip to footer

Choosing The Youtube Video Quality By Using Selenium In Python

I'm having an issue to select the video quality resolution from the youtube video https://www.youtube.com/watch?v=JhdoY-ckzx4. import unittest import time from selenium import webd

Solution 1:

I used this to target the button and open the settings menu.

list = driver_chrome.find_elements_by_class_name("ytp-settings-button")
list[0].click()

I had to use the list because for some reason the class can sometimes appear twice. Using list[0] made it work consistently.

Here is the result:enter image description here

Here is the full working code:

import unittest
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import threading


# This example will help us to open a video on youtube and skip the ads

options = webdriver.ChromeOptions()

classChromeTime(unittest.TestCase):

    defsetUp(self):
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument('--no-sandbox')
        chrome_options.add_argument('--incognito')
        chrome_options.add_argument("disable-popup-blocking")
        #chrome_options.add_argument('--headless')
        self.driver = webdriver.Chrome(options=chrome_options) #/usr/local/bin/chromedriver - Linux Machine
        self.driver.maximize_window()

    deftesting3(self):
        driver_chrome = self.driver

        driver_chrome.get("https://youtube.com")
        print("Opening Youtube")
        driver_chrome.find_element_by_name("search_query").send_keys("peter mckinnon")
        print("Looking for the youtuber")
        driver_chrome.find_element_by_id("search-icon-legacy").click()
        print("Finally, we found your youtuber!")
        time.sleep(5)
        driver_chrome.find_element_by_class_name("style-scope ytd-vertical-list-renderer").click()
        print("Trying to open thee video that you would like to watch")
        time.sleep(10)
        driver_chrome.find_element_by_class_name("ytp-ad-skip-button-container").click()
        print("You're skipping the ads")
        time.sleep(10)
        list = driver_chrome.find_elements_by_class_name("ytp-settings-button")
        list[0].click()
        time.sleep(10)

        print("Initial Page Title is : %s" %driver_chrome.title)
        windows_before  = driver_chrome.current_window_handle
        print("First Window Handle is : %s" %windows_before)


# Anything declared in tearDown will be executed for all test casesdeftearDown(self):
        # Close the browser.
        self.driver.close()

if __name__ == "__main__":
    unittest.main()

Solution 2:

You can use this code :

driver.find_element_by_css_selector('button.ytp-button.ytp-settings-button').click()
driver.find_element_by_xpath("//div[contains(text(),'Quality')]").click()

time.sleep(2)   # you can adjust this time
quality = driver.find_element_by_xpath("//span[contains(string(),'144p')]")
print("Element is visible? " + str(quality.is_displayed()))

quality.click()

Post a Comment for "Choosing The Youtube Video Quality By Using Selenium In Python"