通过在Python中使用硒选择youtube视频质量

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过在Python中使用硒选择youtube视频质量相关的知识,希望对你有一定的参考价值。

我在从youtube视频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() 

我需要知道选择正确的元素需要点击的正确CSS选择器或元素。

我尝试了几个要素,但似乎我什么都没得到。

答案

我用它来定位按钮并打开设置菜单。

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

我不得不使用该列表,因为由于某种原因,该类有时会出现两次。使用list [0]使其始终如一地工作。

这里是结果:enter image description here

这里是完整的工作代码:

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视频质量的主要内容,如果未能解决你的问题,请参考以下文章

在python中使用没有选择标签的硒专门选择谷歌表单下拉列表

如何在 python 中更改网页的 HTML 代码 |硒 [重复]

通过 YouTube 数据 API [Python] 下载非自有视频的隐藏式字幕

将视频添加到 YouTube 上用户的收藏夹/点赞播放列表

通过 Youtube API (python) 检索 Youtube 播放列表信息

python:获取频道的所有 youtube 视频网址