如何使用 selenium 创建播放列表 python

how to create a playplist with selenium python

我正在创建一个脚本来使用 selenium 创建 youtube 播放列表,但我被困在这里,我尝试了很多方法,但所有方法都不起作用!有人能帮我吗?谢谢

<input type="text" class="Mf-jm-Qc-qb d-Rb" aria-label="Search terms" style="width: 325px;">

我试过了:

1

driver.find_element_by_xpath("//input[@class='Mf-jm-Qc-qb d-Rb']").send_keys("https://www.youtube.com/watch?v=NliYy7iqh-U")

2

driver.find_element_by_xpath("//input[@type='text']").send_keys("https://www.youtube.com/watch?v=NliYy7iqh-U")

3

driver.find_element_by_css_selector(".Mf-jm-Qc-qb").send_keys("https://www.youtube.com/watch?v=NliYy7iqh-U")

4

driver.find_element_by_class_name("Mf-jm-Qc-qb d-Rb").send_keys("https://www.youtube.com/watch?v=NliYy7iqh-U")

该元素看起来是一个 dynamic 元素,因此您必须为所需的 elementToBeClickable[= 引入 WebDriverWait 31=],您可以使用以下任一解决方案:

  • 使用CSS_SELECTOR:

    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[aria-label='Search terms'][type='text']"))).send_keys("https://www.youtube.com/watch?v=NliYy7iqh-U")
    
  • 使用XPATH:

    WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@aria-label='Search terms' and @type='text']"))).send_keys("https://www.youtube.com/watch?v=NliYy7iqh-U")
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC