【问题标题】:choosing the youtube video quality by using selenium in Python在 Python 中使用 selenium 选择 youtube 视频质量
【发布时间】:2020-04-19 15:36:45
【问题描述】:

我在从 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 选择器或我需要点击以选择正确元素的元素。

我尝试了几个元素,但似乎我没有成功。

【问题讨论】:

    标签: python selenium testing automation qa


    【解决方案1】:

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

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

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

    结果如下:

    这是完整的工作代码:

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

    【讨论】:

    • 有什么想法可以在 twitch 上选择这个吗? div太多
    【解决方案2】:

    您可以使用此代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-19
      • 1970-01-01
      • 1970-01-01
      • 2012-06-01
      • 2014-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多