在Python中使用硒选择youtube视频质量 [英] choosing the youtube video quality by using selenium in Python

查看:60
本文介绍了在Python中使用硒选择youtube视频质量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从youtube视频中选择视频质量分辨率时遇到问题

以下是完整的工作代码:

 导入单元测试导入时间从硒导入webdriver从selenium.webdriver.common.keys导入密钥从selenium.webdriver.chrome.options导入选项来自selenium.webdriver.common.by导入方式从selenium.webdriver.support.ui导入WebDriverWait从selenium.webdriver.support导入EC的预期条件导入线程#此示例将帮助我们在youtube上打开视频并跳过广告选项= webdriver.ChromeOptions()类ChromeTime(unittest.TestCase):def setUp():chrome_options = webdriver.ChromeOptions()chrome_options.add_argument('-no-sandbox')chrome_options.add_argument('-隐身')chrome_options.add_argument("disable-popup-blocking")#chrome_options.add_argument('-headless')self.driver = webdriver.Chrome(options = chrome_options)#/usr/local/bin/chromedriver-Linux机器self.driver.maximize_window()def testing3(self):driver_chrome = self.driverdriver_chrome.get("https://youtube.com")打印(正在打开Youtube")driver_chrome.find_element_by_name("search_query").send_keys("peter mckinnon")打印(寻找youtuber")driver_chrome.find_element_by_id("search-icon-legacy").click()打印(最后,我们找到了您的youtuber!")time.sleep(5)driver_chrome.find_element_by_class_name(样式范围ytd-vertical-list-renderer").click()打印(尝试打开您想要观看的视频")时间.睡眠(10)driver_chrome.find_element_by_class_name("ytp-ad-skip-button-container").click()打印(您正在跳过广告")时间.睡眠(10)列表= driver_chrome.find_elements_by_class_name("ytp-settings-button")列表[0] .click()时间.睡眠(10)打印(初始页面标题为:%s"%driver_chrome.title)windows_before = driver_chrome.current_window_handleprint(第一个窗口句柄为:%s"%windows_before)#在tearDown中声明的所有内容都将对所有测试用例执行def tearDown(自己):#关闭浏览器.self.driver.close()如果__name__ =="__main__":unittest.main() 

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 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() 

class ChromeTime(unittest.TestCase):

    def setUp(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("/Users/hvaandres/Desktop/Dev_Projects/QA_Testing/Project_Video/chromedriver", options=chrome_options) #/usr/local/bin/chromedriver - Linux Machine
        self.driver.maximize_window()  

    def testing3(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)
        driver_chrome.find_element_by_class_name("ytp-popup ytp-settings-menu").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 cases
    def tearDown(self):
        # Close the browser. 
        self.driver.close()

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

I need to know what is the right CSS selector or element that I need to hit to choose the right element.

I tried several elements, but it seems that I'm not getting anywhere.

解决方案

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:

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()

class ChromeTime(unittest.TestCase):

    def setUp(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()

    def testing3(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 cases
    def tearDown(self):
        # Close the browser.
        self.driver.close()

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

这篇关于在Python中使用硒选择youtube视频质量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆